找到你要的答案

Q:How to make a case sensitive code for file upload by name

Q:如何为文件上传一个区分大小写的代码

So i have more than one image to upload, each one being a new line in my mysql table:

I use this code for file input with name="profile"

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) function makes this MySql query

$sql = "UPDATE `art_$id` SET `path` = \"" . $file_path . "\" WHERE `row` = " . (int)$row;

But i want to repeat this code for profil1, profil2... and change accordingly the row (example : profil4 would change row 4 ), without having to repeat this code for each file='name'.

How would i go about that?

所以我有一个以上的图像上传,每一个被我的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重复代码。

我该怎么做?

answer1: 回答1:

Irrespective of your form have "how many fields as of file and what they are named??" . Following code will upload them one by one ..Check it

 $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);
    }

不管你的窗体有多少个字段和文件名??”。下面的代码将一个接一个上传..检查它

 $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);
    }
answer2: 回答2:

Thanks for your answer, as i have a specific submit button for each form and don't wont to validate all forms at once i used this code :

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);

          }



}

}
}

And for file input i use name="profile1",name="profile2",....,name="profile9"(for this case).

感谢您的答复,因为我有一个特定的提交按钮,每种形式,并不会拒绝验证所有的形式,一旦我使用这个代码:

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 =“Profile1”,名为“profile2”的…,名称=“profile9”(这种情况)。

answer3: 回答3:

Here is the the code which will add 1,2,3 at end of the image file name Bellow function will check the file exist in dir or not and return new file name now you can use the function before change_image function call and get new file name and pass new name in change_image for save.

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;
}

Here is the the code which will add 1,2,3 at end of the image file name Bellow function will check the file exist in dir or not and return new file name now you can use the function before change_image function call and get new file name and pass new name in change_image for save.

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;
}
php  mysql