[英]move_uploaded_file() not working
我正在 php 中編寫一個腳本,用於在服務器上上傳文件。 代碼如下:
$target_path = "uploaded_images/";
$target_path = $target_path . basename( $_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['image']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
但是 function move_uploaded_file()
不起作用,它給出了以下錯誤:
Warning: move_uploaded_file(uploaded_images/Mordent.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpOWVz7o' to 'uploaded_images/Mordent.jpg'
我檢查了文件夾的所有權限,甚至 php.ini 中的安全模式已關閉。
不要在此處使用相對路徑名 ( uploaded_images/Mordent.jpg
)。 PHP 的工作目錄(它將用來將相對路徑轉換為絕對路徑)是什么並不總是很明顯。
如果您嘗試將圖像移動到的目錄與您的腳本位於同一目錄中,請定義$target_path
如下:
$target_path = __DIR__ . '/uploaded_images/';
__DIR__
部分為您提供當前腳本所在目錄的絕對路徑,然后為您提供 append 的“相對”部分。
您的代碼非常好...只需更改您要移動上傳文件的目錄的權限
像 0777 一樣,對於所有讀寫權限,您可以手動執行或通過 php 更改它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.