2016-03-28 124 views
0

我一直在寻找堆栈溢出和其他教程如何使用PHP上传文件。下面的脚本一直运行到if语句的条件是move_uploaded_file($ file_temp,$ file_destination),在这个点它回应“file not uploaded”。图片上传失败使用PHP - move_uploaded_file

包含chmod函数的if语句是我试图在上传文件夹中将文件权限更改为0777(我非常肯定地给出了读取和写入访问权限),因为这已经是许多相关堆栈的建议溢出答案。虽然在这一点上我不认为问题是文件许可。

所以基本上我不知道这有什么问题。帮助表示赞赏:)

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    Your Photo: <input type="file" name="image" /> 
    <input type="submit" name="submit" value="Submit" /> 
</form> 

<?php 
print_r($_FILES); 

// file properties 
$file_name = $_FILES['image']['name']; 
$file_tmp_name = $_FILES['image']['tmp_name']; 
$file_size = $_FILES['image']['size']; 
$file_error = $_FILES['image']['error']; 

// get the extension of the file 
$file_ext = explode('.', $file_name); 
$file_ext = strtolower(end($file_ext)); 

var_dump(file_exists('uploads')); 

// this is false 
if(chmod('uploads', 0777)){ 
    echo " booooh"; 
    } 
    else{ 
    echo "naaah"; 
    } 

$file_name_new = uniqid('', true) . '.' . $file_ext; 
echo "<br>"; 
echo $file_destination = '/uploads/' . $file_name;   

// this is false too 
if(move_uploaded_file($file_temp, $file_destination)) { 
    echo "<br>$file_destination<br>"; 
    echo "hello world<br>"; 
} 
else { 
    echo "<br>file not uploaded<br>"; 
} 

?> 
+0

'var_dump(file_exists('uploads'));'是错误我想你检查存在的文件〜/上传或目录。检查'php.ini'中的选项'open_basedir' http://php.net/manual/en/ini.core.php#ini.open-basedir – Naumov

回答

2

你写:

move_uploaded_file($file_temp, $file_destination); 

$file_temp没有在你的脚本中定义。原始文件路径是$file_tmp_name

move_uploaded_file($file_tmp_name, $file_destination); 

另请注意,您从不使用$file_name_new

附加说明:目标路径必须是绝对文件路径:/uploads/目录必须位于目录树的顶层,而不是文档根目录下。

+0

修复更改为move_uploaded_file($ file_temp,$ file_destination); move_uploaded_file($ file_tmp_name,$ file_destination);并将$ destination_path更改为绝对文件路径。谢谢@ fusion3k – Daniel

+0

不客气。 – fusion3k