我正在尝试在我的本地服务器上使用php上传文件,并且在运行脚本(这是一个非常简单的脚本)后,应该回显信息的页面只是空白。有人可以帮我弄清楚问题是什么吗?我曾尝试阅读许多不同的帖子,但我似乎无法找到解决方案。 感谢
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!";
}
?>
我从中得不到任何东西。只是一个空白页...当我检查tmp DIR时,没有任何东西在那里......有什么建议吗?我正在使用XAMP
编辑:
原来这是两部分。 #1 DIR权限,#2我必须通过服务器的DNS浏览到我的xampp目录。这很奇怪,因为我通常可以通过localhost,它会工作正常。我想问题出在XAMPP本身。
感谢大家的帮助!
答案 0 :(得分:2)
临时目录将为空,除非在上传过程中。除非您自己处理,否则PHP会自动删除所有上传的文件。我建议检查你的服务器和php日志以查找错误消息,并且肯定会在你的脚本中添加一些错误处理。你假设上传成功,这不好:
if ($_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) {
... your code here ...
} else {
die("Upload failed with error code " . $_FILES['uploadedfile']['error']);
}
代码定义为here。
答案 1 :(得分:0)
我实际上只是运行了你的代码,它运行得非常好。您可能遇到的唯一问题是权限,请确保/ tmp目录具有0777权限以允许移动文件。这是我运行的代码:
<?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>