简体   繁体   English

PHP:图像未保存在服务器文件夹和数据库中(作为 URL)

[英]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.

相关问题 PHP / MySQL:无法将图像保存到服务器上的文件夹,但链接 URL 已更新 MySQL 数据库 - PHP / MySQL: Failed to save image to folder at server but link URL is updated MySQL database 直接从URL将图像保存到php中的数据库 - save image to database in php direct from a url 保存上传到数据库中服务器文件夹上的图像的路径 - save the path of image being uploaded on server folder in database 将图像数组上传到服务器文件夹并保存到php数据库中 - upload array of images to server folder and save into database in php 如何在 php 中创建文件夹并将图像保存在我的服务器上? - How can a create a folder and save a image on my server in php? 生成后如何将二维码图像保存到服务器文件夹? - PHP - How to save QR code image to server folder once generated ? - PHP 使用PHP在服务器上的文件夹中打开图像并调整其大小,然后将其保存回来 - open and resize an image from a folder on the server in PHP and save it back PHP:将画布图像上传到服务器,并将图像目录保存到数据库 - PHP: upload a canvas image to server and save image directory to database 将裁剪的图像保存到本地服务器,并将其URL保存到MySQL数据库 - Save cropped image to local server and it URL to MySQL database HTML,PHP,MYSQL,上传图片并将其网址保存到数据库中 - HTML, PHP, MYSQL, Upload image and save it's url into database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM