所以我要上传多张图片,每张图片都是我的mysql表中的新行:
我使用此代码进行文件输入,名称="个人资料"
if (isset($_FILES['profile']) === true) {
if (empty($_FILES['profile']['name']) === true) {
echo 'Please choose a file!';
}else {
$allowed= array('jpg', 'jpeg', 'png')
$file_name = $_FILES['profile']['name'];
$file_exts = explode('.', $file_name);
$file_extn = strtolower(end($file_exts));
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array($file_extn, $allowed) === true) {
change_image($session_user_id, 1, $file_temp, $file_extn);
header('Location: galery.php');
exit();
}else {
echo 'Incorrect file type . allowed files are : ';
echo implode(", ", $allowed);
(change_image)函数生成此MySql查询
$sql = "UPDATE `art_$id` SET `path` = \"" . $file_path . "\" WHERE `row` = " . (int)$row;
但是我想为profil1,profil2重复这段代码......并相应地更改行(例如:profil4会更改第4行),而不必为每个文件重复此代码=' name'
我该怎么做?
答案 0 :(得分:0)
无论您的表单是什么,"文件中有多少字段以及它们的名称是什么?" 。以下代码将逐个上传它们..检查
$rowid=1;
foreach($_FILES as $key=>$image_uploaded)
{
if (isset($image_uploaded[$key]) === true) {
if (empty($image_uploaded[$key]['name']) === true) {
echo 'Please choose a file!';
}else {
$allowed= array('jpg', 'jpeg', 'png')
$file_name = $image_uploaded[$key]['name'];
$file_exts = explode('.', $file_name);
$file_extn = strtolower(end($file_exts));
$file_temp = $image_uploaded[$key]['tmp_name'];
if (in_array($file_extn, $allowed) === true) {
change_image($session_user_id, $rowid++, $file_temp, $file_extn);
//change as you need
header('Location: galery.php');
exit();
}else {
echo 'Incorrect file type . allowed files are : ';
echo implode(", ", $allowed);
}
答案 1 :(得分:0)
感谢您的回答,因为我为每个表单提供了一个特定的提交按钮,并且不会在使用此代码时立即验证所有表单:
for ($row = 1; $row <= 9; $row++) {
if (isset($_FILES["profile" . $row]) === true) {
if (empty($_FILES["profile" . $row]['name']) === true) {
echo 'Please choose a file!';
}else {
$allowed= array('jpg', 'jpeg', 'png');
$file_name = $_FILES["profile" . $row]['name'];
$file_exts = explode('.', $file_name);
$file_extn = strtolower(end($file_exts));
$file_temp = $_FILES["profile" . $row]['tmp_name'];
if (in_array($file_extn, $allowed) === true) {
change_image($session_user_id, $row, $file_temp, $file_extn);
//header('Location: galery.php');
//exit();
}else {
echo 'Incorrect file type . allowed files are : ';
echo implode(", ", $allowed);
}
}
}
}
对于文件输入我使用name =&#34; profile1&#34;,name =&#34; profile2&#34;,....,name =&#34; profile9&#34;(对于这种情况) )。
答案 2 :(得分:0)
以下是在图像文件名末尾添加1,2,3的代码
Bellow函数将检查dir中是否存在文件并返回新文件名,现在您可以在change_image
函数调用之前使用该函数并获取新文件名并在change_image
中传递新名称以进行保存。
function getFileName($fileName, $file_extn){
$fileNameWithoutExt = pathinfo($fileName, PATHINFO_FILENAME);
$i = 1;
while(!file_exists('image/art/' . $fileName)){
$fileName = $fileNameWithoutExt . $i . "." . $file_extn;
$i++;
}
return $fileName;
}