简体   繁体   English

如何使区分大小写的代码按名称上传文件

[英]How to make a case sensitive code for file upload by name

So i have more than one image to upload, each one being a new line in my mysql table: 因此,我要上传多个图像,每个图像都是mysql表中的新行:

I use this code for file input with name="profile" 我将此代码用于名称为“ 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 (change_image)函数使此MySql查询

$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'. 但是我想为profil1,profil2 ...重复此代码,并相应地更改行(例如:profil4将更改行4),而不必为每个文件=“ name”重复此代码。

How would i go about that? 我该怎么办?

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

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). 对于文件输入,我使用name =“ profile1”,name =“ profile2”,....,name =“ profile9”(在这种情况下)。

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. 这是将在图像文件名末尾添加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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM