简体   繁体   中英

Having trouble getting php file uploader to work

I'm having trouble getting a simple php file upload script to work. What happens is I get the Thanks message on my browser, but the file isn't anywhere to be seen.

I've been watching the upload directory and /tmp with inotify to see if anything gets created all but nothing does. Here is the html div that allows selecting the file:

<div>
     <p> Select a file </p>
          <form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                   <label for="file">Filename:</label>
                   <input type="file" name="logfile" id="file"><br/><br/>
                   <input type="submit" name="submit" value="Upload">
           </form>
</div>

So then I try and upload the file:

$uploadDir = "/tmp/";


if(isset($_FILES['logfile']['name'])){

        $uploadFile = $uploadDir . basename($_FILES['logfile']['name']);

        print "<p>{$_FILES['logfile']['name']}</p>\n";
        print "<p>{$uploadFile}</p>\n";

        # Delete if already exists
        if (file_exists($uploadFile)) {
                unlink($uploadFile);
        }

        // Recieve the file
        if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
                displayMessage("Thanks");
        }
        else {
                # Couldnt move the file
                displayMessage("Error moving file");
        }
}

What happens is I get the message "Thanks" in my browser but no file. I've checked permissions, checked my nginx log - no errors at all. It looks like it is successful but nothing is being copied?

Edit:

Even this fails:

$myfile = fopen("/tmp/testfile.txt", "w") or die("Unable to open file!");
$current = "John Smith\n";
fwrite($myfile, $current);
fclose($myfile);

Basically I cant write to /tmp for some reason, even though nginx is not reporting any error. I've also tried the same script under apache, with the same results.

drwxrwxrwt 18 root root 460 Aug 20 10:56 /tmp/

It's working fine for me. can you check the upload directory? where you have kept your tmp folder? just use if the same place of the file $uploadDir="tmp/";

<?php
$uploadDir = "";
if(isset($_FILES['logfile']['name'])){

    $uploadFile = $uploadDir . basename($_FILES['logfile']['name']);

    print "<p>{$_FILES['logfile']['name']}</p>\n";
    print "<p>{$uploadFile}</p>\n";

    # Delete if already exists
    if (file_exists($uploadFile)) {
       unlink($uploadFile);
    }
    // Recieve the file
    if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
            echo "Thanks";
    }
    else {
            # Couldnt move the file
            echo "Error moving file";
    }
}
?>
<div>
 <p> Select a file </p>
      <form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
               <label for="file">Filename:</label>
               <input type="file" name="logfile" id="file"><br/><br/>
               <input type="submit" name="submit" value="Upload">
       </form>
</div>

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