Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php上传脚本赢得';行不通_Php_File_Upload - Fatal编程技术网

php上传脚本赢得';行不通

php上传脚本赢得';行不通,php,file,upload,Php,File,Upload,我想在我的主页上传图片。 我正在使用w3schools编写的脚本来执行此操作: 这是upload.html <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="

我想在我的主页上传图片。 我正在使用w3schools编写的脚本来执行此操作:

这是upload.html

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
我是php的初学者,不知道问题出在哪里。 也许有人能帮我解决这个问题。:)

  • 通过在命令行执行以下操作,检查是否可以上载文件:

    chmod-R 777/var/www/uploads/

  • 然后试着上传一些东西。如果这样做有效,您就知道这是一个权限问题。但您不想将uploads文件夹保留在777权限(这将允许所有人访问)

  • 如果是权限问题,下面是我喜欢的权限处理方式:

    chown[您的用户名]:www-data-R/var/www/uploads/

    chmod-R 775/var/www/uploads/


  • 这会将文件夹的组分配更改为www数据,这通常是apache在Ubuntu linux服务器上运行的方式。

    您对“/var/www/uploads/”文件夹的权限是什么,它需要由您的Web服务器用户拥有或可写,最常见的是www-data或apacheMake确保“/var/www/uploads/”的权限设置为
    755
    您的
    move\u uploads\u file
    函数返回false。如果文件不是有效的上载文件,或者文件是有效的上载文件,但由于某些原因无法移动,则会发生这种情况。检查您对要上载到的文件夹的权限。也许你需要确保你有读写权限。我在哪里可以更改文件夹的权限?服务器在Debian 7上运行。编辑:刚刚在Filezilla上找到它。但问题仍然存在。你有菲利齐拉吗?只需右键单击文件夹并选择权限,然后键入755
    <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !="jpeg" && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    ?>
    
    File is an image - image/jpeg.
    Sorry, there was an error uploading your file.
    Array ( [fileToUpload] => Array ( [name] => Test Bild Website Upload.jpg [type] => image/jpeg [tmp_name] => /tmp/phpyxW0TX [error] => 0 [size] => 2999 ) )