我试图在上传表单中再放一个字段,以便能够在一个表单中上传 3 张图像。我遇到的问题是其中两个图像必须在数据库中保存与第三个图像不同的路径。例如,image_1
和image_2
被保存到http://example.com/img/
,第三个图像保存到../images/
目前,当我用数据库中的所有 3 张图像填写字段时,我为它们保存了这个名称 - 4649d81924f1b17db1444d72ee271f6c-Array
这是 html 表单
<form role="form" action="" method="post" enctype="multipart/form-data">
<!-- File Button -->
<div class="form-group">
<label for="image">image</label>
<input value="<?php echo !empty($image)?$image:'';?>" id="image" name="file[]" multiple="multiple" class="input-file" type="file">
</div>
<!-- File Button -->
<div class="form-group">
<label for="rest_img_big">Image 2</label>
<input value="<?php echo !empty($rest_img_big)?$rest_img_big:'';?>" id="rest_img_big" name="file[]" multiple="multiple" class="input-file" type="file">
</div>
<!-- File Button -->
<div class="form-group">
<label for="web_image">Image 3</label>
<input value="<?php echo !empty($web_image)?$web_image:'';?>" id="web_image" name="file[]" multiple="multiple" class="input-file" type="file">
</div>
</form>
这是用于图像上传的PHP部分
if ( !empty($_POST) && isSet($_POST['submit']))
{
define('MAX_FILE_SIZE', 20000000430);
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
// keep track post values
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time()) . '-' .$fileName;
// save image path
$path = "../../img/".$randName;
$forDB = "http://example/img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
}
}
// update data
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE images set image = ?, rest_img_big = ?, web_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($forDB,$forDB,$path,$user_id));
因此,任何建议如何解决此问题并能够同时上传3张图像?
更新:当前代码
if ( !empty($_POST) && isSet($_POST['submit']))
{
define('MAX_FILE_SIZE', 20000000430);
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
// keep track post values
for($i=0; $i < count($_FILES['file']['name']); $i++){
$fileName = $i.$_FILES['file']['name'][$i]; //get unique name
$tmpName = $_FILES['file']['tmp_name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileType = $_FILES['file']['type'][$i];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time()) . '-' .$fileName;
// save image path
$web = "../../img/".$randName;
$smallImageForApp = "http://example/img/".$randName;
$bigImageForApp = "http://example/img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $web);
if (!$result)
{
echo "Error uploading image file";
exit;
}
}
else {
// update data
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE images set image = ?, rest_img_big = ?, web_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($smallImageForApp,$bigImageForApp,$web,$user_id));
}
更新2:var_dump($_FILES);
array(1) {
["file"]=> array(5)
{
["name"]=> array(3)
{
[0]=> string(21) "23.jpg_1418028921.jpg"
[1]=> string(15) "_1418028891.jpg"
[2]=> string(6) "78.jpg"
}
["type"]=> array(3)
{
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(3)
{
[0]=> string(14) "/tmp/phpfqqJG3"
[1]=> string(14) "/tmp/phptp9zcF"
[2]=> string(14) "/tmp/phpTkmuIg"
}
["error"]=> array(3)
{
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
}
["size"]=> array(3)
{
[0]=> int(25878)
[1]=> int(25878)
[2]=> int(35864)
}
}
}
array(1)
{
//same thing 3 times
}
通过循环逐个上传文件
for($i=0; $i < count($_FILES['file']['name']); $i++){
$fileName = $_FILES['file']['name'][$i];
$tmpName = $_FILES['file']['tmp_name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileType = $_FILES['file']['type'][$i];
// more stuff . . . . . .
}
您当前的代码只会上传单个图像,对于 3 张图片,您需要遍历 $_FILES['file']
并上传它,这是如何走的路
for($i=0; $i < count($_FILES['file']['name']); $i++){
$fileName = $i.$_FILES['file']['name'][$i]; //get unique name
$tmpName = $_FILES['file']['tmp_name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileType = $_FILES['file']['type'][$i];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time()) . '-' .$fileName;
// save image path
//check if its 3rd image change the path to '../images/' folder
$path = ($ === 2) ? '../images/' : "../../img/".$randName;
$forDB = "http://example/img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
} else {
// update data in db here, only on upload
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE images set image = ?, rest_img_big = ?, web_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($forDB,$forDB,$path,$user_id));
}
}
}
您的$fileName
4649d81924f1b17db1444d72ee271f6c-Array
混乱,因为您将array
附加到字符串,最后Array
显示
注意:不要在数据库中存储图像的完整 url $forDB = "http://example/img/".$randName;
您应该只存储名称$forDB = $randName;
这样您就可以将应用程序从一个域移动到另一个域,但图像不会损坏。