简体   繁体   English

调试将图片文件上传到文件夹

[英]Debugging uploading picture files into a folder

I am following this tutorial: http://www.w3schools.com/php/php_file_upload.asp . 我正在关注本教程: http : //www.w3schools.com/php/php_file_upload.asp And I have copied and pasted the code for both the PHP and the HTML. 而且我已经复制并粘贴了PHP和HTML的代码。 And in a folder, I have upload.html , php.ini (with file_uploads = On ), upload.php and a folder called uploads where all the uploaded files are supposed to go in, I think. 我认为,在一个文件夹中,我有upload.htmlphp.ini (带有file_uploads = On ), upload.php和一个名为uploads的文件夹,应该将所有已上传的文件放入其中。

And when I press submit, everything seems to work, namely, the success message is echoed, "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded." 当我按Submit时,一切似乎都起作用,即成功消息回显为"The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded." "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded." . However, no file seems to appear inside of the uploads folder. 但是, uploads文件夹内似乎没有文件。

Also, when I try to upload the same file, I get the error message, echo "Sorry, file already exists."; 另外,当我尝试上传相同的文件时,收到错误消息, echo "Sorry, file already exists."; , which is weird because I don't see the file in my uploads folder. ,这很奇怪,因为在我的uploads文件夹中看不到该文件。

I am currently using FireFTP to store all my code and the files that I am uploading. 我目前正在使用FireFTP存储我的所有代码和要上传的文件。

Here is my code for upload.html : 这是我的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>

And here is my upload.php : 这是我的upload.php

<?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.";
    }
}
?>

Also, someone told me that PHP is a server side language. 另外,有人告诉我PHP是服务器端语言。 What does that actually mean? 这实际上是什么意思? I tried to read some stuff online but its so full of so much jargon. 我试图在线阅读一些东西,但它充满了很多术语。 Does it mean that I have to use something like FireFTP to be able to use languages like PHP? 这是否意味着我必须使用FireFTP之类的东西才能使用PHP之类的语言?

And I can't just open up my textfile using chrome and be able to use PHP? 而且我不能只使用chrome打开文本文件并能够使用PHP? Like, I right click my textfile containing my code and do "open with chrome"? 就像,我右键单击包含我的代码的文本文件,然后“用chrome打开”吗? Does this also have to do with web-hosting? 这也与虚拟主机有关吗?

Try This Code 试试这个代码

   $file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
        $file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
        $upload_exts = end(explode(".", $_FILES["fileToUpload"]["name"]));
                    if ((($_FILES["fileToUpload"]["type"] == "image/gif")
                    || ($_FILES["fileToUpload"]["type"] == "image/jpeg")
                    || ($_FILES["fileToUpload"]["type"] == "image/png")
                    || ($_FILES["fileToUpload"]["type"] == "image/pjpeg"))
                    && ($_FILES["fileToUpload"]["size"] < 2000000)
                    && in_array($upload_exts, $file_exts))
                            {
                                if ($_FILES["fileToUpload"]["error"] > 0)
                                {
                                    header("Location:dashboard.php?err=imgpro1");
                                    //echo "Return Code: " . $_FILES["fileToUpload"]["error"] . "<br>";
                                }
                                else
                                {
                                    $tempupname = time().$_FILES["fileToUpload"]["name"];
                                    $imgpathtostore="uploads/".$tempupname;
                                    $imgpathtostoreDB="uploads/".$tempupname;
                                    // Enter your path to upload file here
                                    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imgpathtostore);
                                    }
                                    }
                                    else
                                            {
                                                header("Location: index.php?err=imgpro");
                                            }
      }

您需要将uploads文件夹权限设置为读取,写入和删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM