简体   繁体   中英

php file upload not working - blank page

I'm trying to upload a file using php on my local server and after running the script (which is a very simple one) the page that should echo information is just blank. Can someone please help me figure out what the problem is? I've tried reading up on many different posts but I can't seem to find a solution. Thanks

html

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

php

<?php 
// Where the file is going to be placed 
$target_path = "tmp/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$target_path = "tmp/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

I'm not getting anything from this. Just a blank page...and when I check the tmp DIR nothing is in there... any suggestions? I am using XAMP

EDIT:

Turns out it was two parts. #1 DIR permission, and #2 I had to browse to my xampp directory through the DNS for the server. Which is strange because I could usually just go through localhost and it would work fine. I guess the problem was with XAMPP itself.

Thanks for everyone's help!

The temp dir would be empty except during the time the upload is proceeding. PHP auto-deletes any uploaded files unless you deal with them yourself. I'd suggest checking your server and php logs to look for error messages, and definitely add some error handling to your script. You're assuming the upload has succeeded, which is not good:

if ($_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) {
    ... your code here ...
} else {
    die("Upload failed with error code " . $_FILES['uploadedfile']['error']);
}

The codes are defined here .

I actually just ran your code and it works perfectly fine. the only issue you might be having is the permission, make sure /tmp directory has 0777 permission to allow the file to be moved. here is the code I ran :

<?php 
// Where the file is going to be placed 
if(isset($_FILES['uploadedfile'])){

$target_path = "img/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$target_path = "img/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
}
?>


<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

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