简体   繁体   中英

Why won't my file upload with my PHP upload script?

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";
  }
?> 

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/

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM