PHP多个文件上传并创建缩略图不进入每个循环
这是到目前为止我所掌握的内容,对于篇幅我深表歉意,但这为您提供了完整的图片:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
foreach ($_FILES['file']['name'] as $key => $file)
作为 for 循环$_FILES['file']['tmp_name'][ if 子句中
$key] 在参考 的
use
foreach ($_FILES['file']['name'] as $key => $file)
as for loop$_FILES['file']['tmp_name'][$key]
in if clausefor reference