0

First I asked this question here: How do I change the file name before sending to upload_file.php?

I updated my upload_file.php script with the suggestion, but when I run it, it doesn't work. Using the form from the above link, when I go to upload the script, the echo's seem correct, but when I FTP to the directory the file is supposed to be, the directory is empty? Could someone shine some light on this? I want the file to be uploaded to '/var/www/eedis/fax/uploads/'.

My upload_file.php script looks like this...

<?php
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ($_FILES["file"]["type"] == "application/pdf")
#&& ($_FILES["file"]["size"] < 20000)
#&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/var/www/eedis/fax/uploads/" . $_POST['number'] . '.pdf');
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
3
  • You probably haven't set write permissions on the target directory for the web server user. Look at your server error log. Skype consultancy $100/hr! Commented Nov 12, 2013 at 6:21
  • chmod 77 /var/www/eedis/fax/uploads/ Commented Nov 12, 2013 at 6:26
  • Can you post the form code please Commented Nov 12, 2013 at 6:31

3 Answers 3

1

Create a PHP file named upload.php for uploading process .

create a folder with the name "gallery" (or what you want) in your project directory , we want the uploaded files go to it.

$uploadDir = './gallery/';

for returning the result of the uploading process.

$result = 0;

now processing user input and get the file extension, then create a unique file name for the uploaded file (because different users may upload a file with similar file names)

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

//get the file extension
$ext = substr(strrchr($fileName, "."), 1);

// make the random file name
$randName = md5(rand() * time());

// creating the unique file name for the uploaded file
$filePath = $uploadDir . $randName . '.' . $ext;

if(@move_uploaded_file($tmpName, $filePath)) {

    //move the uploaded file from server's temp directory to your gallery path
    $result = 1;
}
sleep(1);
}

to create PHP/AJAX full system for uploading your files and images read this article :

http://blueingenuity.com/articles/4/creating-a-photo-uploader-using-ajax-and-php/

Sign up to request clarification or add additional context in comments.

Comments

0

Problem fixed. It was just a permission one. On to the next one!

Comments

0

Have you used enctype="multipart/form-data" in your form? Also look at the folder permission.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.