move_uploaded_file - 图像的文件名?

move_uploaded_file  - 图像的文件名?

问题描述:

The Script:

<?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/>';
        }
    }
}
?>

This script also uses javascript for multiple image upload.

This script works fine, though when the multiple files get moved into the "uploads" folder the file names come out like this:

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

How to solve this issue?

脚本: strong> p>

   &lt;?php 
if(isset($ _ POST ['submit'])){
 $ j = 0;  //索引上传图像的变量
 
 $ target_path =“uploads /”;  //声明上传图像的路径
 for($ i = 0; $ i&lt; count($ _ FILES ['file'] ['name']); $ i ++){//循环从数组中获取单个元素 
 
 $ validextensions = array(“jpeg”,“jpg”,“png”);  //允许的扩展
 $ ext = explode('。',basename($ _ FILES ['file'] ['name'] [$ i])); //从dot(。)
中爆炸文件名 $ file_extension = end($ ext);  //将扩展名存储在变量
 
 $ target_path = $ target_path中。  md5(uniqid())。  “”  。  $ ext [count($ ext) -  1]; //使用新的图像名称设置目标路径
 $ j = $ j + 1; //根据数组中的文件增加上传图像的数量
  
 if(($ _FILES [“file”] [“size”] [$ i]&lt; 100000)//可以上传大约100kb的文件。
&amp;&amp; in_array($ file_extension,$ validextensions))  {
 if(move_uploaded_file($ _ FILES ['file'] ['tmp_name'] [$ i],$ target_path)){//如果文件移动到上传文件夹
 echo $ j。  ')。&lt; span id =“noerror”&gt;图片上传成功!。&lt; / span&gt;&lt; br /&gt;&lt; br /&gt;'; 
}其他{//如果文件未移动。  
 echo $ j。  ')。&lt; span id =“error”&gt;请再试一次!。&lt; / span&gt;&lt; br /&gt;&lt; br /&gt;'; 
} 
}其他{//如果文件大小 和文件类型不正确。
 echo $ j。  ')。&lt; span id =“error”&gt; ***无效的文件大小或类型***&lt; / span&gt;&lt; br /&gt;&lt; br /&gt;'; 
} 
} 
  n} 
?&gt; 
  code>  pre> 
 
 

此脚本还使用javascript进行多个图片上传。 p>

此脚本运行正常, 但是当 多个 em> strong>文件移入“uploads”文件夹时,文件名会如下所示: p>

 “  2f594262c1f8fb56c39cc01d4543bcb9.jpg “
” 个2f594262c1f8fb56c39cc01d4543bcb9.jpgf00008a16882f01d2bd7ed6d9805a4bf.jpg “
” 个2f594262c1f8fb56c39cc01d4543bcb9.jpgf00008a16882f01d2bd7ed6d9805a4bf.jpge967f7fbaea4e2aee9b6f56067739aed.jpg“
 代码>  PRE> 
 
 

如何解决这个问题? p > div>

Reset your $target_path inside the for loop. Right now you're just continually appending to it.

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