上传php脚本错误移动图像到文件夹 [英] Upload php script error moving image to folder

查看:32
本文介绍了上传php脚本错误移动图像到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的错误:

(在第 55 行添加了一条注释)一个想法是修改脚本以不必移动文件,但不知道如何做.不确定这是否可行,因为我需要将它输出到与文件最初具有相同名称的上传文件夹.

(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?

注意:我不能给自己任何权限,它在本地主机上运行良好,但我必须将其上传到 Web 服务器并进行测试,我只能修改 php 而不能更改任何服务器设置.

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.

推荐答案

来自你的错误.

我建议修复文件夹权限才能写入.

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

这篇关于上传php脚本错误移动图像到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆