doushua7737 2015-12-10 04:42
浏览 51

上传php脚本错误将图像移动到文件夹

Here is my error:

enter image description here

(Added a comment to line 55) One idea would be to modify the script to not have to move the file, but no idea how to do that. Not sure if that would even work because I need it to be outputted to the uploads folder with the same name the file had initially.

<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">   
    Title:<br>
    <input type="text" name="imgTitle" required><br><br>
    Upload a File:  <input type="file" name="imgUploaded" id="imgUploaded"><br><br>
    Comments: <br>
    <textarea  name="imgDesc" rows="4" cols="35"required></textarea><br><br>

    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
<?php
if (isset($_POST["submit"]) ) { 

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["imgUploaded"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imgTitle =  $_POST['imgTitle'];
$imgDesc =  $_POST['imgDesc'];
$servername = "sn";
$username = "un";   
$password = "pw";
$dbname = "db";
echo  $imgDesc;
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["imgUploaded"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
if ($_FILES["imgUploaded"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["imgUploaded"]["tmp_name"],$target_file)) // this is line 55 
{
        $imgPath = $_FILES["imgUploaded"]["name"];
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        $sql = "INSERT INTO BrianJones (sqlName, sqlDesc, sqlPath)
        VALUES ('$imgTitle', '$imgDesc', '$imgPath')";
        if (mysqli_query($conn, $sql)) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
        mysqli_close($conn);

        echo "The file ". basename( $_FILES["imgUploaded"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
}
?>

Not sure how to fix this, obviously it's an error when trying to move my temp image to the uploads folder because I don't have permission. How can I get around this?

Note: I cannot give myself any permissions, it works fine on localhost but I have to upload it to a web server and test, I can only modify the php and not change any server settings.

  • 写回答

2条回答 默认 最新

  • dongpa2000 2015-12-10 04:48
    关注

    From what your Error.

    I recommend to fix folder permission to be able to write.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看