简体   繁体   English

图片上传失败

[英]Image upload failing in PHP

So I'm trying to set up an image upload from a form via php on my localhost and after running the code and connecting to the database okay, I'm getting the error for the upload. 所以我试图通过本地主机上的php通过表单设置图像上传,并在运行代码并连接到数据库之后,我收到了上传错误。 Since all of the other parts of the form are working after a section by section check, I'll just add the html for the upload input and its relevant php script. 由于表单的所有其他部分在逐节检查后都可以正常工作,因此我只添加用于上传输入的html及其相关的php脚本。

<input type="file" name="image" id="image-select" />

And the portion of the php that has to deal with the image upload and verification after upload: php中必须处理图片上传和上传后验证的部分:

$image = $_FILES ['image']['name'];
$type = @getimagesize ($_FILES ['image']['tmp_name']);

//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error code " . $_FILES['image']['error']);
}

//Where the file will be placed
$target_path = "uploads/";

// Add the original filename to target path
$path= $target_path . basename($image);

// Check if the image is invalid before continuing
 if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
    echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
    die("This is not a valid image file!");
 } else {
        move_uploaded_file($_FILES['image']['tmp_name'], $path);
 }

So error appears once the code hits the upload process. 因此,一旦代码到达上传过程,就会出现错误。 I was attempting to upload a small PNG file The relevant code I added is also in their respective orders when they appear in the script. 我试图上载一个小的PNG文件。当它们出现在脚本中时,添加的相关代码也按其各自的顺序排列。

Just a wild stab in the dark here as you haven't provided your form tag but you have remembered the enctype attribute haven't you? 此处只是一片黑暗,因为您没有提供表单标签,但您还记得enctype属性吗?

Sorry I don't have 50 reputation otherwise I would ask this as a comment. 抱歉,我没有50信誉,否则我会以此作为评论。

<form enctype='multipart/form-data' action=''>
<input type="file" name="image" id="image-select" />
</form>

Also you should check if the file uploaded OK before calling getimagesize() (not that this would be the source of your issue) 另外,您还应该在调用getimagesize()之前检查文件是否已上传成功(不是这会成为问题的根源)

如果您使用的是PHP5Imagick ,那么您可以尝试使用类似以下的内容:

$image->readImageFile($f);

can you please put error here. 你能在这里输入错误吗? If error means that after submit page file is not being upload. 如果错误表示提交页面文件后未上传。 then please check your form enctype. 然后请检查您的表单密码。 see below an example 参见以下示例

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image-select" />
<input type="submit" value="Submit">
</form>

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

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