我尝试在上传表格中添加一个字段,以便能够以一种形式上传3张图片。我遇到的问题是两个图像必须在第三个图像的DB中保存不同的路径。
例如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
}
答案 0 :(得分:1)
您当前的代码只会上传单张图片,而您需要循环播放$_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
显示
注意:不要在db $forDB = "http://example/img/".$randName;
中存储图片的完整网址,您应该只存储名称$forDB = $randName;
,以便您可以从域名移动应用程序到域,但图像不会破碎。
答案 1 :(得分:1)
您需要通过循环逐个上传文件
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 . . . . . .
}