1

我正在使用以下脚本进行简单的文件上传:

$errors = '';
$target_path = "[PATH HERE]";

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

if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
     $errors = "The file ".  basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
     $errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
    }

出于某种原因,我上传文件时出错并且文件类型未显示。它似乎只获取没有扩展名的文件名(即“test”而不是“test.pdf”)。我确定这很简单,但是我做错了什么?

4

2 回答 2

0

如果您检查 files 数组中的错误元素,您可能会发现它不是 0 的值。如果没有出错,错误应该是 0。否则,将错误存储的值与 PHP 文档进行比较,以确定发生了什么问题。

于 2012-04-11T18:38:04.673 回答
0

也许您输入的路径错误(结束斜杠),或者 php 没有写入目录的权限。

<?php 
error_reporting(E_ALL); // Show some errors

$target_path = "/var/www/somesite.com/uploads/"; // Would require a ending slash 

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

if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
    $errors = "The file ".  basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
    $errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}

?>
于 2012-04-11T18:40:00.367 回答