javascript - move_uploaded_file - 图像的文件名?

标签 javascript php

脚本:

<?php
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 

    $target_path = "uploads/"; //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] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            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/>';
        }
    }
}
?>

此脚本还使用 JavaScript 来上传多个图像。

此脚本工作正常,但当多个文件移至“上传”文件夹时,文件名如下所示:

"2f594262c1f8fb56c39cc01d4543bcb9.jpg"
"2f594262c1f8fb56c39cc01d4543bcb9.jpgf00008a16882f01d2bd7ed6d9805a4bf.jpg"
"2f594262c1f8fb56c39cc01d4543bcb9.jpgf00008a16882f01d2bd7ed6d9805a4bf.jpge967f7fbaea4e2aee9b6f56067739aed.jpg"

如何解决这个问题?

最佳答案

在 for 循环内重置 $target_path。现在你只是不断地追加它。

for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
    $target_path = "uploads/"; //Declaring Path for uploaded images

关于javascript - move_uploaded_file - 图像的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325155/

相关文章:

php - 如何有条件地覆盖 Eloquent 模型中的值?

javascript - 在 JSX 中使用 For 循环

javascript - 使用 Jquery 或 JavaScript 向 css 菜单添加延迟

javascript - Photoswipe 无法读取 photoswipe.js 第 1070 行未定义的属性 'center'

php - Laravel 奇怪的 PDO fetchMode 问题

php - Laravel 尝试将数据传递给 View 时 undefined variable

javascript - Google 图表不显示 IE-9

javascript - 网站导航在 WKWebview 中不起作用

javascript - 如何在使用 react(redux) 渲染之前设置状态

php - 追踪流量来源