doubi4531 2017-02-20 04:06
浏览 38
已采纳

PHP文件路径命名问题

I tried creating a script to upload multiple photos, rename photo, and add database entry. The database filename entry is correct however the image upload filename is compounding the jpg name.

ie. Database entry is correctly naming files: photo_1, photo_2, photo_3

The files are being uploaded named incorrectly: photo_1.jpg, photo_1.jpgphoto_2.jpg, photo_1.jpgphoto_2.jpgphoto_3.jpg

Not sure how to resolve this as my database entry is correct.

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

    $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", "JPG", "PNG", "JPEG");  //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





        //MYSQL Handling

            mysql_query( "INSERT INTO post_images(`image_filename`, `image_post_id`) VALUES('0', '".addslashes($image_post_id)."')" );
                            $new_id = mysql_insert_id();

                            $target_path = $target_path . "post_". $new_id . "." . strtolower($ext[count($ext) - 1]);//set the target path with a new name of image
                            mysql_query( "UPDATE post_images SET image_filename='post_".addslashes($new_id).".".strtolower($ext[count($ext) - 1])."' WHERE image_id='".addslashes($new_id)."'" );












        $j = $j + 1;//increment the number of uploaded images according to the files in array       

      if (($_FILES["file"]["size"][$i] < 1000000) //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/>';
        }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dpjs2005 2017-02-20 05:27
    关注

    Your problem is with this line in your for loop:

    $target_path = $target_path . "post_". $new_id . "." . strtolower($ext[count($ext) - 1]);
    

    This code essentially says "Append the new filename to the previous value", which obviously is not what you want.

    To fix, just remove the $target_path on the right of the =:

    $target_path = "post_". $new_id . "." . strtolower($ext[count($ext) - 1]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题