PHP多个文件上传并创建缩略图不进入每个循环

发布于 2025-01-04 19:06:42 字数 4673 浏览 0 评论 0原文

这是到目前为止我所掌握的内容,对于篇幅我深表歉意,但这为您提供了完整的图片:

if(count($_FILES['file']['name'])) 
            {

            foreach ($_FILES['file']['name'] as $key => $file)
                {



            $add="../uploads/photogallery/".time().'_'.$file;

            $storefile = time().'_'.$file;

                             echo $storefile; // THIS WORKS

                if(move_uploaded_file ($_FILES['file']['tmp_name'][$key],$add))
                {

                                     // CODE IS NOT GETTING INTO THIS AREA,

                    //database insert here

                    //mysql_query ($query);

                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_added">';
                }
                else
                {
                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_not_added">';
                }

            ///////// Start the thumbnail generation//////////////

            $n_width=150;          // Fix the width of the thumb nail images
            $n_height=150;         // Fix the height of the thumb nail imaage

            $tsrc="../uploads/photogallery/thumbnail/".time().'_'.$file;

            if (!($_FILES['file']['type'][$key] =="image/pjpeg" || $_FILES['file']['type'][$key] == "image/gif" || $_FILES['file']['type'][$key] == "image/png") || ($_FILES["file"]["type"][$key] == "image/jpeg") && ($_FILES["file"]["size"][$key] < 2097152))
                {
                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_requirements_not_met">';
                }

            /////////////////////////////////////////////// Starting of GIF thumb nail creation///////////

            if (@$_FILES['file']['type'][$key] =="image/gif")
                {
                    $im=ImageCreateFromGIF($add);

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);                  // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    if (function_exists("imagegif")) 
                    {
                        Header("Content-type: image/gif");
                        ImageGIF($newimage,$tsrc);
                    }

                    elseif (function_exists("imagejpeg")) 
                    {
                        Header("Content-type: image/jpeg");
                        ImageJPEG($newimage,$tsrc);
                    }
                    elseif (function_exists("imagepng")) 
                    {
                        Header("Content-type: image/png");
                        ImagePNG($newimage,$tsrc);
                    }
                }

            ////////////// starting of JPG thumb nail creation//////////

            if ($_FILES['file']['type'][$key] == "image/pjpeg")
                {
                    $im=ImageCreateFromJPEG($add); 

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);             // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);                 

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    ImageJpeg($newimage,$tsrc);
                }

            if ($_FILES['file']['type'][$key] == "image/png")
                {
                    $im=ImageCreateFromPNG($add); 

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);             // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);                 

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    ImagePNG($newimage,$tsrc);
                }
            }
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_requirements_not_met">';
        }

这是表单的 HTML 部分:

<input class="input" name="file[]" type="file" id="file" multiple=""/>

是的,表单具有正确的 enctype。请参阅上面代码中的我的评论,其中写着“THIS WORKS”(此有效),而“Code IS NOT”(代码不可行)等。这就是我的问题所在。它正在进入 for every 循环部分,因为我可以 echo $storefile 并获取带有时间戳的正确文件名,但它不会进入 if (move_uploaded_..) 部分并跳到 echo 照片要求未满足部分。

任何帮助将不胜感激。我没有收到任何 PHP 或 mysql 错误。

Here is what I have so far, I apologize for the length but this gives you the complete picture:

if(count($_FILES['file']['name'])) 
            {

            foreach ($_FILES['file']['name'] as $key => $file)
                {



            $add="../uploads/photogallery/".time().'_'.$file;

            $storefile = time().'_'.$file;

                             echo $storefile; // THIS WORKS

                if(move_uploaded_file ($_FILES['file']['tmp_name'][$key],$add))
                {

                                     // CODE IS NOT GETTING INTO THIS AREA,

                    //database insert here

                    //mysql_query ($query);

                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_added">';
                }
                else
                {
                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_not_added">';
                }

            ///////// Start the thumbnail generation//////////////

            $n_width=150;          // Fix the width of the thumb nail images
            $n_height=150;         // Fix the height of the thumb nail imaage

            $tsrc="../uploads/photogallery/thumbnail/".time().'_'.$file;

            if (!($_FILES['file']['type'][$key] =="image/pjpeg" || $_FILES['file']['type'][$key] == "image/gif" || $_FILES['file']['type'][$key] == "image/png") || ($_FILES["file"]["type"][$key] == "image/jpeg") && ($_FILES["file"]["size"][$key] < 2097152))
                {
                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_requirements_not_met">';
                }

            /////////////////////////////////////////////// Starting of GIF thumb nail creation///////////

            if (@$_FILES['file']['type'][$key] =="image/gif")
                {
                    $im=ImageCreateFromGIF($add);

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);                  // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    if (function_exists("imagegif")) 
                    {
                        Header("Content-type: image/gif");
                        ImageGIF($newimage,$tsrc);
                    }

                    elseif (function_exists("imagejpeg")) 
                    {
                        Header("Content-type: image/jpeg");
                        ImageJPEG($newimage,$tsrc);
                    }
                    elseif (function_exists("imagepng")) 
                    {
                        Header("Content-type: image/png");
                        ImagePNG($newimage,$tsrc);
                    }
                }

            ////////////// starting of JPG thumb nail creation//////////

            if ($_FILES['file']['type'][$key] == "image/pjpeg")
                {
                    $im=ImageCreateFromJPEG($add); 

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);             // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);                 

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    ImageJpeg($newimage,$tsrc);
                }

            if ($_FILES['file']['type'][$key] == "image/png")
                {
                    $im=ImageCreateFromPNG($add); 

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);             // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);                 

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    ImagePNG($newimage,$tsrc);
                }
            }
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_requirements_not_met">';
        }

And here is the HTML portion of the form:

<input class="input" name="file[]" type="file" id="file" multiple=""/>

Yes the form has the correct enctype. See my comments in the code above where it says THIS WORKS and CODE IS NOT..etc. That is where my issue is. It's getting inside the for each loop part since I can echo $storefile and get the correct filename with the time stamp, but it won't get into the if (move_uploaded_..) portion and skips to the echo photo requirements not met portion.

Any help would be greatly appreciated. I am not getting any PHP or mysql errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2025-01-11 19:06:42

使用 foreach ($_FILES['file']['name'] as $key => $file) 作为 for 循环

$_FILES['file']['tmp_name'][ if 子句中

$key] 在参考

例如,假设文件名 /home/test/review.html 和
/home/test/xwp.out 已提交。在这种情况下,
$_FILES['userfile']['name'][0] 将包含值 review.html,
和 $_FILES['userfile']['name']1 将包含值 xwp.out。
同样,$_FILES['userfile']['size'][0] 将包含 review.html
文件大小等等。

$_FILES['用户文件']['名称'][0], $_FILES['用户文件']['tmp_name'][0],
$_FILES['userfile']['size'][0] 和 $_FILES['userfile']['type'][0] 是
也设置。

use foreach ($_FILES['file']['name'] as $key => $file) as for loop

$_FILES['file']['tmp_name'][$key] in if clause

for reference

For instance, assume that the filenames /home/test/review.html and
/home/test/xwp.out are submitted. In this case,
$_FILES['userfile']['name'][0] would contain the value review.html,
and $_FILES['userfile']['name']1 would contain the value xwp.out.
Similarly, $_FILES['userfile']['size'][0] would contain review.html's
file size, and so forth.

$_FILES['userfile']['name'][0], $_FILES['userfile']['tmp_name'][0],
$_FILES['userfile']['size'][0], and $_FILES['userfile']['type'][0] are
also set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文