2012-04-11 45 views
1

我用下面的脚本做一个简单的文件上传:PHP简单的文件上传

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

+5

由于某些原因,人们坚持从不做适当的错误检查。确保$ _FILES ['uploadFile'] ['error']没有错误,然后确保move_uploaded_file中的路径正确,然后如果仍然存在问题,请确保Web服务器的用户正在运行在目录上写入权限。 – Corbin 2012-04-11 18:35:29

+1

并检查'upload_max_filesize' php ini变量是否足够大。 – mamadrood 2012-04-11 18:37:30

+0

'var_dump($ _ FILES)' – Travesty3 2012-04-11 18:39:12

回答

0

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

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']; 
} 

?> 
+0

我再次检查了路径,并且结尾的斜杠在那里。我也检查了权限,他们也是正确的。 – bdev 2012-04-11 18:58:48