PHP简单文件上传

时间:2012-04-11 18:29:29

标签: php file-upload

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

$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”)。我确信这很简单,但我做错了什么?

2 个答案:

答案 0 :(得分:0)

如果检查files数组中的error元素,你可能会发现它的值不是0。如果没有出错,错误应为0。否则,将存储在error中的值与PHP文档进行比较,以确定出错的地方。

答案 1 :(得分: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'];
}

?>