PHP多次上传保留文件名

PHP多次上传保留文件名,php,file-upload,Php,File Upload,我有一个脚本,我用来上传多个图像,但这个脚本每次重命名我的图像,我不想要它。如何保留原始图像名称? 这是我的代码: <?php if (isset($_POST['submit'])) { $j = 0; // Variable for indexing uploaded image. $target_path = "../images/gallery/"; // Declaring Path for uploaded images. for ($i

我有一个脚本,我用来上传多个图像,但这个脚本每次重命名我的图像,我不想要它。如何保留原始图像名称? 这是我的代码:

<?php
if (isset($_POST['submit'])) {
    $j = 0;     // Variable for indexing uploaded image.
    $target_path = "../images/gallery/";     // Declaring Path for uploaded images.
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
        // Loop to get individual element from the array
        $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
        $ext = explode('.', basename($_FILES['file']['name'][$i]));   // Explode file name from dot(.)
        $file_extension = end($ext); // Store extensions in the variable.
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
        $j = $j + 1;      // Increment the number of uploaded images according to the files in array.
        if (($_FILES["file"]["size"][$i] < 10000000) && in_array($file_extension, $validextensions)) {     // Approx. 100kb files can be uploaded.
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
                // If file moved to uploads folder.
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            } else {     //  If File Was Not Moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {     //   If File Size And File Type Was Incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}
?>


在这里,您正在使用
md5(uniqid())
更改文件名,而不是像
$\u FILES['file']['name'][$i]

这样的文件名。我已经编辑了您的代码。这应该行得通

<?php
if (isset($_POST['submit'])) {
$j = 0;     // Variable for indexing uploaded image.
$target_path = "../images/gallery/";     // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
    // Loop to get individual element from the array
    $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
    $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    $target_path .= $_FILES['file']['name'];
    $j = $j + 1;      // Increment the number of uploaded images according to the files in array.
    if (($_FILES["file"]["size"][$i] < 10000000) && in_array($ext, $validextensions)) {     // Approx. 100kb files can be uploaded.
        if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
            // If file moved to uploads folder.
            echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
        } else {     //  If File Was Not Moved.
            echo $j. ').<span id="error">please try again!.</span><br/><br/>';
        }
    } else {     //   If File Size And File Type Was Incorrect.
        echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
    }
}
}
?>

您必须删除此行

$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];
只需添加
$\u文件['file']['name'][$i]

定义

uniqid()-uniqid()函数根据微时间(以微秒为单位的当前时间)生成唯一的ID

您可以在
php.ini
文件上更改
max\u file\u上载的值。默认情况下,它是
20
。你想增加多少就增加多少

此外,您还可以尝试此代码

HTML

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Multiple File Ppload with PHP</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  <input type="submit" value="Upload" />
</form>
</body>
</html>

$target\u path=$target\u path。md5(uniqid()。"." . $分机[计数($ext)-1];//使用图像的新名称设置目标路径??????我必须换那一行吗?我怎么做?谢谢,请检查我的答案。嗨,谢谢你的回答。我尝试了您的解决方案,但我得到了以下结果:原始文件名:image-1.jpg,image-2.jpg,image-3.jpg更改结果:image-1.jpg.jpg.jpgimage-2.jpg.jpg,image-1.jpg.jpgimage-2.jpg.jpgimage-3.jpg我只需要保留每个文件名:)好的,我得到了您需要删除“$ext[count($ext)-1]”的问题这部分代码将得到您想要的输出。那么,它应该是这样的吗$目标路径=$target\u路径$_文件['file']['name'][$i];我得到了这个错误:“警告:pathinfo()希望参数1是字符串,数组在第8行1的/web/htdocs/www.xxx.com/xxx/upload/upload.php中给出)。***无效的文件大小或类型***更改此行:$ext=pathinfo($\u FILES['file']['name'],pathinfo扩展名);改为:$ext=pathinfo($\u FILES['file'['name'][$I],pathinfo扩展名);没关系,但我不想重命名图像名称,如果您希望图像的名称相同,它会使用md5重命名图像。为什么不保留它们的原样($_FILES['file']['name']);因为我在网上找到了这段代码,我不知道如何更改它。你能帮我吗?谢谢:)对不起,但你能更好地解释一下吗?$target\u path=$target\u path.md5(uniqid())。“..$ext[count($ext)-1];只需添加$\u文件['file']['name'][$I]
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Multiple File Ppload with PHP</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  <input type="submit" value="Upload" />
</form>
</body>
</html>
$valid_formats = array("jpg", "png", "jpeg");
$max_file_size = 10000000; 
$path = "../images/gallery/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
}