PHP多文件上载将同一图像上载5次,而不是所有文件
嘿,伙计们,我真的在为我的代码而挣扎。当我尝试上传多张图片时,它会将同一张图片上传5次,而不是同时上传两张图片。感谢您的帮助。 谢谢你抽出时间 这是var\u dumb($\u文件) 以下是我的上传功能:PHP多文件上载将同一图像上载5次,而不是所有文件,php,file-upload,Php,File Upload,嘿,伙计们,我真的在为我的代码而挣扎。当我尝试上传多张图片时,它会将同一张图片上传5次,而不是同时上传两张图片。感谢您的帮助。 谢谢你抽出时间 这是var\u dumb($\u文件) 以下是我的上传功能: if(count($_FILES['userfile'])){ foreach($_FILES['userfile'] as $file){ $alt=mysqli_real_escape_string($conn, $_POST['alt']); echo '<pre>'
if(count($_FILES['userfile'])){
foreach($_FILES['userfile'] as $file){
$alt=mysqli_real_escape_string($conn, $_POST['alt']);
echo '<pre>';
var_dump($_FILES);
echo '</pre>';
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
}
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($_FILES["userfile"]["type"] == "image/gif")
||($_FILES["userfile"]["type"]=="image/jpeg")
||($_FILES["userfile"]["type"]=="image/png")
||($_FILES["userfile"]["type"]=="image/pjpeg")
&& in_array($extension, $allowedExts)))
{
$fp = fopen($tmpName, 'r');
$content =fread($fp, filesize($tmpName));
$SourceImage = imagecreatefromstring($content);
$SourceWidth = imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$DestWidth=100;
$DestHeight=130;
if ($SourceHeight> $SourceWidth)
{$ratio = $DestHeight / $SourceHeight;
$newHeight = $DestHeight;
$newWidth = $sourceWidth * $ratio;
}
else
{
$ratio = $DestWidth / $SourceWidth;
$newWidth = $DestWidth;
$newHeight = $SourceHeight * $ratio;
}
$DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
ob_start();
imagejpeg($DestinationImage);
$BinaryThumbnail = ob_get_contents();
ob_end_clean();
$thumb = addslashes($BinaryThumbnail);
$content = addslashes($content);
fclose($fp);
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed');
echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>";
unlink ($_FILES['username']['tmp_name']);
}else{
echo "<script>alert('Please upload an image');location.replace('upload.php');</script>";
}
}
}
}
if(计数($\u文件['userfile'])){
foreach($\u FILES['userfile']作为$file){
$alt=mysqli\u real\u escape\u字符串($conn,$\u POST['alt']);
回声';
变量转储($\u文件);
回声';
如果($_文件[“用户文件”][“错误”]==0)&($_文件[“用户文件”][“大小”]>0))
{
$fileName=$_文件['userfile']['name'];
$tmpName=$\u文件['userfile']['tmp\u名称'];
$fileSize=$_文件['userfile']['size'];
$fileType=$_文件['userfile']['type'];
}
$allowedExts=数组(“jpg”、“jpeg”、“gif”、“png”);
$extension=end(分解(“.”,$_文件[“用户文件”][“名称]);
如果(($_文件[“用户文件”][“类型”]=“图像/gif”)
||($_文件[“用户文件”][“类型”]=“图像/jpeg”)
||($_文件[“用户文件”][“类型”]=“图像/png”)
||($_文件[“用户文件”][“类型”]=“图像/pjpeg”)
&&在数组中($extension$allowedExts)))
{
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$SourceImage=imagecreatefromstring($content);
$SourceWidth=imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$DestWidth=100;
$DestHeight=130;
如果($SourceHeight>$SourceWidth)
{$ratio=$DestHeight/$SourceHeight;
$newHeight=$DestHeight;
$newWidth=$sourceWidth*$ratio;
}
其他的
{
$ratio=$DestWidth/$SourceWidth;
$newWidth=$DestWidth;
$newHeight=$SourceHeight*$ratio;
}
$DestinationImage=ImageCreateTureColor($newWidth,$newHeight);
imagecopyresampled($DestinationImage、$SourceImage、0,0,0、$DestWidth、$DestHeight、$SourceHeight、$SourceWidth);
ob_start();
图像JPEG($DestinationImage);
$BinaryThumbnail=ob_get_contents();
ob_end_clean();
$thumb=addslashes($BinaryThumbnail);
$content=addslashes($content);
fclose($fp);
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$content=addslashes($content);
fclose($fp);
mysqli_query($conn,“插入到文件(用户名、名称、大小、内容、类型、链接、alt、缩略图)值(“$username”、“$fileName”、“$fileSize”、“$content”、“$fileType”、“$alt”、“$thumb”)或die('Error,query failed');
echo“警报('文件已上载');location.replace('upload.php');”;
取消链接($_文件['username']['tmp_name']);
}否则{
echo“警报('Please upload a image');location.replace('upload.php');”;
}
}
}
}
以下是上传表格:
if(count($_FILES['userfile'])){
for($i=0;$i<count($_FILES['userfile']);$i++) {
$alt=mysqli_real_escape_string($conn, $_POST['alt']);
echo '<pre>';
var_dump($_FILES);
echo '</pre>';
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'][$i];
$tmpName = $_FILES['userfile']['tmp_name'][$i];
$fileSize = $_FILES['userfile']['size'][$i];
$fileType = $_FILES['userfile']['type'][$i];
}
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($_FILES["userfile"]["type"] == "image/gif")
||($_FILES["userfile"]["type"]=="image/jpeg")
||($_FILES["userfile"]["type"]=="image/png")
||($_FILES["userfile"]["type"]=="image/pjpeg")
&& in_array($extension, $allowedExts)))
{
$fp = fopen($tmpName, 'r');
$content =fread($fp, filesize($tmpName));
$SourceImage = imagecreatefromstring($content);
$SourceWidth = imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$DestWidth=100;
$DestHeight=130;
if ($SourceHeight> $SourceWidth)
{$ratio = $DestHeight / $SourceHeight;
$newHeight = $DestHeight;
$newWidth = $sourceWidth * $ratio;
}
else
{
$ratio = $DestWidth / $SourceWidth;
$newWidth = $DestWidth;
$newHeight = $SourceHeight * $ratio;
}
$DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
ob_start();
imagejpeg($DestinationImage);
$BinaryThumbnail = ob_get_contents();
ob_end_clean();
$thumb = addslashes($BinaryThumbnail);
$content = addslashes($content);
fclose($fp);
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed');
echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>";
unlink ($_FILES['username']['tmp_name'][$i]);
}else{
echo "<script>alert('Please upload an image');location.replace('upload.php');</script>";
}
}
}
}
上传文件
上载文件:
Alt文本:
在循环内部使用foreach中的变量,而不是$\u文件['userfile']
还可以在
$\u文件上使用var\u dump
检查它是如何发送的。如果一次上载多个文件,服务器很可能只接受一个文件。我曾经遇到过这个问题,我通过ajax文件上传修复了它。Ajax文件上传也非常有用,因为用户不必等待所有文件都传输到服务器。你可以用这个 您可以尝试for loop
而不是foreach
…如下所示:
if(计数($\u文件['userfile'])){
对于($i=0;$i=0))
{
$fileName=$_文件['userfile']['name'][$i];
$tmpName=$\u文件['userfile']['tmp\u名称'][$i];
$fileSize=$_文件['userfile']['size'][$i];
$fileType=$_文件['userfile']['type'][$i];
}
$allowedExts=数组(“jpg”、“jpeg”、“gif”、“png”);
$extension=end(分解(“.”,$_文件[“用户文件”][“名称]);
如果(($_文件[“用户文件”][“类型”]=“图像/gif”)
||($_文件[“用户文件”][“类型”]=“图像/jpeg”)
||($_文件[“用户文件”][“类型”]=“图像/png”)
||($_文件[“用户文件”][“类型”]=“图像/pjpeg”)
&&在数组中($extension$allowedExts)))
{
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$SourceImage=imagecreatefromstring($content);
$SourceWidth=imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$DestWidth=100;
$DestHeight=130;
如果($SourceHeight>$SourceWidth)
{$ratio=$DestHeight/$SourceHeight;
$newHeight=$DestHeight;
$newWidth=$sourceWidth*$ratio;
}
其他的
{
$ratio=$DestWidth/$SourceWidth;
$newWidth=$DestWidth;
$newHeight=$SourceHeight*$ratio;
}
$DestinationImage=ImageCreateTureColor($newWidth,$newHeight);
imagecopyresampled($DestinationImage、$SourceImage、0,0,0、$DestWidth、$DestHeight、$SourceHeight、$SourceWidth);
ob_start();
图像JPEG($DestinationImage);
$BinaryThumbnail=ob_get_contents();
ob_end_clean();
$thumb=addslashes($BinaryThumbnail);
$content=addslashes($content);
fclose($fp);
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$content=addslashes($content);
fclose($fp);
mysqli_query($conn,“插入到文件(用户名、名称、大小、内容、类型、链接、alt、缩略图)值(“$username”、“$fileName”、“$fileSize”、“$content”、“$fileType”、“$alt”、“$thumb”)或die('Error,query failed');
echo“警报('文件已上载');location.replace('upload.php');”;
取消链接($_文件['username']['tmp_name'][$i]);
}否则{
echo“警报('Please upload a image');location.replace('upload.php');”;
}
}
}
}
是的,我会考虑使用JS/AJAX解决方案,而不是PHP。使用php上载多个文件有点麻烦,因为加载时间长,而且代码运行时页面刷新时间长
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>Upload a file</h1>
<form method="post" enctype="multipart/form-data" action="process.php">
<div id="filediv">
<div id="imagefiles">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile[]" type="file" id="userfile" multiple></label>
<label>Alt Text: <input name="alt" type="text"></label>
</div>
</div>
<br>
<input type="button" value="Add Another File" onclick="copyDiv()"/>
<input name="UploadFile" type="submit" />
</form>
if(count($_FILES['userfile'])){
for($i=0;$i<count($_FILES['userfile']);$i++) {
$alt=mysqli_real_escape_string($conn, $_POST['alt']);
echo '<pre>';
var_dump($_FILES);
echo '</pre>';
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'][$i];
$tmpName = $_FILES['userfile']['tmp_name'][$i];
$fileSize = $_FILES['userfile']['size'][$i];
$fileType = $_FILES['userfile']['type'][$i];
}
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($_FILES["userfile"]["type"] == "image/gif")
||($_FILES["userfile"]["type"]=="image/jpeg")
||($_FILES["userfile"]["type"]=="image/png")
||($_FILES["userfile"]["type"]=="image/pjpeg")
&& in_array($extension, $allowedExts)))
{
$fp = fopen($tmpName, 'r');
$content =fread($fp, filesize($tmpName));
$SourceImage = imagecreatefromstring($content);
$SourceWidth = imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$DestWidth=100;
$DestHeight=130;
if ($SourceHeight> $SourceWidth)
{$ratio = $DestHeight / $SourceHeight;
$newHeight = $DestHeight;
$newWidth = $sourceWidth * $ratio;
}
else
{
$ratio = $DestWidth / $SourceWidth;
$newWidth = $DestWidth;
$newHeight = $SourceHeight * $ratio;
}
$DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
ob_start();
imagejpeg($DestinationImage);
$BinaryThumbnail = ob_get_contents();
ob_end_clean();
$thumb = addslashes($BinaryThumbnail);
$content = addslashes($content);
fclose($fp);
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed');
echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>";
unlink ($_FILES['username']['tmp_name'][$i]);
}else{
echo "<script>alert('Please upload an image');location.replace('upload.php');</script>";
}
}
}
}