php php上传图片到目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13470482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:30:45  来源:igfitidea点击:

Php upload image to directory

php

提问by bush man

Ive been experimenting with the upload capability of php, I have been looking at this tutorial on w3school.

我一直在试验php的上传功能,我一直在看w3school上的这个教程。

http://www.w3schools.com/php/php_file_upload.asp

I installed this script on MYDOMAIN.com/New.html


<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

And then I put this snippet on MYDOMAIN.com/upload_file.php

然后我把这个片段放在 MYDOMAIN.com/upload_file.php

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Now there is a very high chance that im just a really confused person but what I assume that is suppose to do is take the file I put into the New.html file and if it is a image file upload it onto my directory but instead its just outputting Invalid file Im not sure what to do any help is great hope to hear back soon thank you

现在很有可能我只是一个非常困惑的人,但我认为应该做的是将我放入 New.html 文件中的文件,如果它是一个图像文件,将它上传到我的目录中,而不是它的只是输出无效的文件我不知道该怎么做任何帮助非常希望能尽快收到回复谢谢

回答by Ivo

The problem of your script is that you are probably trying to upload a file higher that your limit is! You specified a really low max filesize! Print out your $_FILES var to get information about what's wrong ;)

您的脚本的问题是您可能正在尝试上传高于您的限制的文件!您指定的最大文件大小非常低!打印出您的 $_FILES var 以获取有关错误的信息;)

However, in order to move the file to your folder you still need to use move_uploaded_file:

但是,为了将文件移动到您的文件夹,您仍然需要使用move_uploaded_file

$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'uploads/';

if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
    $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file

    if ( in_array( end($info), $allow) ) // is this file allowed
    {
        if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
        {
            // the file has been moved correctly
        }
    }
    else
    {
        // error this file ext is not allowed
    }
}

回答by GaurabDahal

best way to upload images i think would be to copy an image and store inside a folder and then store the new location of image into a databse.

我认为上传图像的最佳方法是复制图像并存储在文件夹中,然后将图像的新位置存储到数据库中。

to move the file we can use move_uploaded_file function

要移动文件,我们可以使用 move_uploaded_file 函数

$oldpath = $_FILES['image']['tmp_name'];
$newpath ="new_folder_location/".$_FILES['image']['name'];
move_uploaded_file($oldpath, $newpath);

回答by MrCode

Your validation code is checking the extension, mime type and file size.

您的验证代码正在检查扩展名、mime 类型和文件大小。

You are probably uploading a file with uppercase characters in the extension. When you check the extension, you checking it case sensitive, .JPGis a valid image file extension, so you should make your check case insensitive:

您可能正在上传扩展名中包含大写字符的文件。当您检查扩展名时,您检查它是否区分大小写,.JPG是一个有效的图像文件扩展名,因此您应该使检查不区分大小写:

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array(strtolower($extension), $allowedExts))
         // ^ added strtolower()

If that isn't the immediate problem then the file is too big or has the wrong mime type. To debug, view the files array with:

如果这不是直接的问题,那么文件太大或有错误的 MIME 类型。要调试,请使用以下命令查看 files 数组:

print_r($_FILES);

回答by aleation

I would say the problem is in that If, a lot of conditions there (also "invalid file" is the Else part of that if), try to simplify it to track the problem, at first glance I would say it's because of the size checking:

我会说问题在于,如果那里有很多条件(“无效文件”也是该 if 的 Else 部分),请尝试简化它以跟踪问题,乍一看我会说这是因为大小检查:

&& ($_FILES["file"]["size"] < 20000)

that's around 20Kb, quite small for an image if you are uploading a photo, try to put a higher value or to take out that condition and see if the script works

大约 20Kb,对于上传照片的图像来说非常小,请尝试设置更高的值或取出该条件并查看脚本是否有效

回答by Botond Balázs

This happens because the following expression evaluates to false:

发生这种情况是因为以下表达式的计算结果为 false:

(($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)

Which means that either

这意味着要么

  • The file type is incorrect (it is not a GIF/JPEG/PNG/PNJPEG file)
  • The file is bigger than 20000 bytes
  • The file extension is incorrect (it is not any of "jpg", "jpeg", "gif", "png")
  • 文件类型不正确(不是 GIF/JPEG/PNG/PNJPEG 文件)
  • 文件大于 20000 字节
  • 文件扩展名不正确(不是“jpg”、“jpeg”、“gif”、“png”中的任何一个)

My guess would be the file size - increase it drastically. Change it to the following to allow sizes up to 10 MB:

我的猜测是文件大小 - 大幅增加它。将其更改为以下内容以允许最大 10 MB 的大小:

&& ($_FILES["file"]["size"] < 10485760)

The best way to see the problem is to add this line to the beginning of your script:

查看问题的最佳方法是将此行添加到脚本的开头:

var_dump($_FILES);

回答by mk_man

I agree with answer of Ivo but I think that you may make change of next piece of code like this:

我同意 Ivo 的回答,但我认为您可以像这样更改下一段代码:

$typePicture = array('jpg','png','jpeg','gif');

// Allow certain file formats

if($typePicture[0] != "jpg" || $typePicture[1] != "png" || $typePicture[2] != "jpeg" || $typePicture[3] != "gif" ) 
{
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}