我想上传多个具有唯一文件名的图片,这是我的html代码在
之下 <input name="upfile1" type="file">
<input name="upfile2" type="file">
<input name="upfile3" type="file">
在这里,我想用缩略图将这三张图片上传到3个不同的文件夹中。任何人都可以帮助我并提供解决方案吗?
答案 0 :(得分:1)
尝试以下代码:
> <?php //set where you want to store files //in this example we keep
> file in folder upload //$HTTP_POST_FILES['ufile']['name']; = upload
> file name //for example upload file name cartoon.gif . $path will be
> upload/cartoon.gif
$path1="upload1/".$HTTP_POST_FILES['ufile']['name'][0];
$path2="upload2/".$HTTP_POST_FILES['ufile']['name'][1];
$path3="upload3/".$HTTP_POST_FILES['ufile']['name'][2];
>
> //copy file to where you want to store file
> copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
> copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
> copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
>
> //$HTTP_POST_FILES['ufile']['name'] = file name
> //$HTTP_POST_FILES['ufile']['size'] = file size
> //$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size:".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type:".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">"; echo "<P>";
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">"; echo "<P>";
> echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";
>
> // Use this code to display the error or success.
>
> $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
> $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
> $filesize3=$HTTP_POST_FILES['ufile']['size'][2];
>
> if($filesize1 && $filesize2 && $filesize3 != 0) { echo "We have
> recieved your files"; }
>
> else { echo "ERROR....."; }
>
> //////////////////////////////////////////////
>
> // What files that have a problem? (if found)
>
> if($filesize1==0) { echo "There're something error in your first
> file"; echo "<BR />"; }
>
> if($filesize2==0) { echo "There're something error in your second
> file"; echo "<BR />"; }
>
> if($filesize3==0) { echo "There're something error in your third
> file"; echo "<BR />"; }
>
> ?>
答案 1 :(得分:0)
我写了一个类来轻松处理文件,这些文件正在提交一段时间......
https://github.com/homer6/altumo/blob/master/source/php/Form/UploadedFile.php
用法:
$uploaded_files = \Altumo\Form\UploadedFile::loadFiles();
例如。 $ uploaded_files值:
array(
'myfile1' => object(Altumo\Form\UploadedFile)
'nested_form_name' = array(
'nested_file_1' => object(Altumo\Form\UploadedFile)
'nested_file_2' => object(Altumo\Form\UploadedFile)
)
)
这些值与以下形式一致:
<input type="file" name="myfile1" />
<input type="file" name="nested_form_name[nested_file_1]" />
<input type="file" name="nested_form_name[nested_file_2]" />
希望有所帮助
答案 2 :(得分:-1)
demo:使用最后一个插入ID在数据库中多次插入图像
if (isset($_FILES['uploadimage']['name'])) {
$file_name_all = "";
for ($i = 0; $i < count($_FILES['uploadimage']['name']); $i++) {
$tmpFilePath = $_FILES['uploadimage']['tmp_name'][$i];
if ($tmpFilePath != "") {
$path = "assets/img/roomimages/"; // create folder
$name = $_FILES['uploadimage']['name'][$i];
$size = $_FILES['uploadimage']['size'][$i];
list($txt, $ext) = explode(".", $name);
$file = time().substr(str_replace(" ", "_", $txt), 0);
$info = pathinfo($file);
$filename = 'image'. $file.".".$ext;
}
if (move_uploaded_file($_FILES['uploadimage']['tmp_name'][$i], $path.$filename)) {
$sql_image = mysqli_query($con, "insert into roomimages(postroom_id,type,uploadimage) values('$last','$type','".$filename."')") or die(mysqli_error($con));
}
}
}