[英]PHP / MySQL: Failed to save image to folder at server but link URL is updated MySQL database
[英]PHP: Image not save in server folder and database (as URL)
目前,我創建了一個允許用戶上傳照片的 PHP 系統。 現在,我收到此錯誤。
PHP 警告:move_uploaded_file(images/before_4.png):無法打開流:C:\\inetpub\\wwwroot\\tgotworker_testing\\pages\\dashboard\\engineer\\view_task\\update_photo_before.php 中第 28 行沒有這樣的文件或目錄 PHP 警告:move_uploaded_file (): 無法將 'C:\\Windows\\Temp\\phpA952.tmp' 移動到 C:\\inetpub\\wwwroot\\tgotworker_testing\\pages\\dashboard\\engineer\\view_task\\update_photo_before.php 中的 'images/before_4.png' 第 28 行
下面是我的代碼。
<?php
require_once '../../../../config/configPDO.php';
$report_id = $_POST['report_id'];
$last_insert_id = null;
//Allowed file type
$allowed_extensions = array("jpg","jpeg","png");
//File extension
$ext = strtolower(pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION));
//Check extension
if(in_array($ext, $allowed_extensions)) {
$defID = "before_" . $report_id;
$imgPath = "images/$defID.png";
$ServerURL = "http://172.20.0.45/tgotworker_testing/android/$imgPath";
$query = "UPDATE ot_report SET photo_before = '$ServerURL', time_photo_before = GETDATE() WHERE report_id = :report_id";
$sql = $conn->prepare($query);
$sql->bindParam(':report_id', $report_id);
$sql->execute();
if ($sql){
move_uploaded_file($_FILES['uploadFile']['tmp_name'], $imgPath); //line 28
echo "<script>alert('Saved')</script>";
header("Location: view_task.php?report_id=".$_POST['report_id']);
}else{
echo "Error!! Not Saved";
}
} else {
echo "<script>alert('File not allowed')</script>";
header("Location: view_task.php?report_id=".$_POST['report_id']);
}
?>
誰能幫我解決這個問題?
您正在第 28 行使用 file_get_contents() :
move_uploaded_file(file_get_contents($_FILES['uploadFile']['name']),$imgPath);
但是函數 move_uploaded_file 不需要它的結果。 你可以這樣做:
move_uploaded_file($_FILES['uploadFile']['tmp_name'],$imgPath);
對於絕對路徑的第二個問題:在 $imgPath 中,也許您應該使用它:
$imgPath="C:/inetpub/wwwroot/tgotworker_testing/android/images/$defID.png";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.