File uploading only working with some images (class.upload.php)

痞子三分冷 提交于 2019-12-11 17:59:11

问题


I've tried the native way of uploading images with PHP, but my friend suggested me to the class.upload.php library, which still had the same results as before. I can only upload certain images without them having that little icon that means that the browser can't find the image, but what's weird is that when I download the "invalid" images to my computer, they're fine. About 50% of images actually do work, but the rest just show the appropriate size that they should have but no image. Here is my code (my html form has a file input type called filename:

$handle = new upload($_FILES['filename']);

if ($handle->uploaded)   
{
   if ($file_src_size < 20000)
   {
        $handle->file_new_name_body = "test";  
        $handle->image_convert = 'jpg';
        $handle->process('/');  

        echo "<img src = \"test.jpg\" />";
    }
    else echo "Files must be 20 kb or under";
}
else echo "Upload failed, please try again";

回答1:


The fact that you can download the images and re-display locally them suggests that the problem is not with the upload script. An alternative problem could be that the images are not properly saved for web viewing. For example, just because a file ends in .jpg and can be viewed locally does not mean that it is recognizable by a browser. A CMYK jpeg cannot be viewed by many browsers.




回答2:


You need to debug your code as something is going wrong, and only someone with access to your computer can tell you why.

1) The library you're using has error detection code

if ($handle->processed) {
       echo 'image resized';
       $handle->clean();
} else {
   echo 'error : ' . $handle->error;
}

2) You should be able to look at the processed image data, and figure out why the browser is not able to parse it as an image. If you can give a URL for an invalid image I would be able to tell you what's wrong with it.

3) Writing files to the root directory by calling "process('/');" is bad. You should make a directory to hold files that come from other people, to avoid having potential problems when some uploads an image called 'index.php'.



来源:https://stackoverflow.com/questions/10940527/file-uploading-only-working-with-some-images-class-upload-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!