简体   繁体   English

在php后端上传图片失败

[英]Uploading image failing on php back-end

I'm trying to implement a small update profile pic form. 我正在尝试实现一个小的更新配置文件图片表单。

<form method="post" action="<?php echo $filename;?>" name="change_picture_form" id="change_picture_form" enctype="multipart/form-data">
    <input type="hidden" name="action" value="change_picture" />
    <input type="file" name="new_user_picture">
    <input type="submit" class="submitButton" value="Save Changes"/>
</form>

The target php file has the following code: 目标php文件具有以下代码:

echo $_FILES['new_user_picture']['size']." ";
echo $_FILES['new_user_picture']['tmp_name']." ";
echo $_FILES['new_user_picture']['name']." ";
echo $_FILES['new_user_picture']['error']." ";
echo $_FILES['new_user_picture']['type']." ";
$picture_uploaded = $_FILES["new_user_picture"]["tmp_name"];
if( is_uploaded_file( $picture_uploaded ) ) {
    $imagesize = getimagesize( $picture_uploaded );
    switch( $imagesize[2] ) {
        case IMAGETYPE_PNG:
              $extension = '.png';
              echo "<script>console.log('Reached here!!')</script>";
              try {
                  $image_original = imagecreatefrompng( $picture_uploaded );
                  if (!$image_original)
                      echo '<script>console.log("not image original")</script>';
              } catch(Exception $e) {
                  echo "<script>console.log('Error!!')</script>";
              }
              break;
        case IMAGETYPE_JPEG: ....
        ...
    }
}

Here, I have similar code for many image types. 在这里,我对许多图像类型都有类似的代码。 I tested this code by trying to uploading a png image. 我通过尝试上传png图片测试了此代码。 The first 5 echo statements display expected results - the size, error value of zero, the name, the type and the temp name. 前5个echo语句显示预期结果-大小,错误值为零,名称,类型和临时名称。

I get "Reached here!!" 我得到“到达这里!!” on my console. 在我的控制台上。 imagecreatefrompng , however, seems to crash silently. 但是, imagecreatefrompng似乎崩溃了。 Try-catch somehow doesn't seem to catch the error. 尝试捕获似乎无法捕获该错误。

Help? 救命? Thanks! 谢谢!

I haven't handled file uploads in PHP in forever, but is it possible that this line: 我还没有永远用PHP处理文件上传,但是以下行是否有可能:

$picture_uploaded = $_FILES["new_user_picture"]["tmp_name"]

should be 应该

$picture_uploaded = $_FILES["new_user_picture"] ? $picture_uploaded = $_FILES["new_user_picture"]吗?

The first line, as is, should be getting you the uploaded file's file name, whereas the second line (the edited line above) should be a reference to the file itself. 第一行应该是为您提供上载文件的文件名,而第二行(上面的编辑行)应该是对文件本身的引用。

HTH. HTH。

EDIT: Well, seeing as how my answer is incorrect... does this help? 编辑:好吧,我的答案是不正确的...这有帮助吗? http://www.php.net/manual/en/function.imagecreatefromstring.php . http://www.php.net/manual/zh/function.imagecreatefromstring.php There's a helper function in the user notes/comments that might simplify what you're doing 用户注释/注释中有一个帮助程序功能,可以简化您的工作

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

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