[英]PHP: Image not save in server folder and database (as URL)
Currently, I created a PHP system that can allow the user to upload a photo.目前,我创建了一个允许用户上传照片的 PHP 系统。 Now, I get this error.现在,我收到此错误。
PHP Warning: move_uploaded_file(images/before_4.png): failed to open stream: No such file or directory in C:\\inetpub\\wwwroot\\tgotworker_testing\\pages\\dashboard\\engineer\\view_task\\update_photo_before.php on line 28 PHP Warning: move_uploaded_file(): Unable to move 'C:\\Windows\\Temp\\phpA952.tmp' to 'images/before_4.png' in C:\\inetpub\\wwwroot\\tgotworker_testing\\pages\\dashboard\\engineer\\view_task\\update_photo_before.php on line 28 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 行
Below is my code.下面是我的代码。
<?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']);
}
?>
Can anyone help me to solve this problem?谁能帮我解决这个问题?
You are using file_get_contents() on this line 28 :您正在第 28 行使用 file_get_contents() :
move_uploaded_file(file_get_contents($_FILES['uploadFile']['name']),$imgPath);
but the function move_uploaded_file is not needing the result of it.但是函数 move_uploaded_file 不需要它的结果。 You can just do like that instead :你可以这样做:
move_uploaded_file($_FILES['uploadFile']['tmp_name'],$imgPath);
For your second problem with absolute path: in $imgPath maybe you should use this instead:对于绝对路径的第二个问题:在 $imgPath 中,也许您应该使用它:
$imgPath="C:/inetpub/wwwroot/tgotworker_testing/android/images/$defID.png";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.