上传文件的名称不能分开

时间:2015-09-17 13:48:00

标签: php

我仍然是pdo或php的初学者,所以非常抱歉,如果这个问题听起来很愚蠢。

我正在制作一个能够从一个输入上传多个图像的代码,并且它已成功上传到该文件夹​​。而在db中我只插入图像路径,并且也是成功的。

现在这就是问题的开始...... 我正在使用microtime()作为上传文件的新名称。上传的第一张图片没有问题,但从第二张图片开始,名称将从上传的第一张图片的名称继续。 这是第一张图片的文件路径/名称:uploads / 14424949591995.jpg 和第二张图片:uploads / 14424949591995.jpg14424949592555.jpg

'uploads'是文件夹的名称btw

正如你所看到的,我似乎无法将名称分开以取其名称为uploads / 14424949592555.jpg

这是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
</head>

<body>

<h2>Multiple Image Upload Form</h2>
<form enctype="multipart/form-data" action="" method="post">
First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB.
<div id="filediv"><input name="file[]" type="file" id="file" multiple="multiple" /></div>

<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>
//php is placed here
</body>
</html>

和php:

 <?php
function microtime_name()
        {
        list($usec, $sec) = explode(" ", microtime());
        return ($usec + $sec)*10000;
        }
if (isset($_POST['submit'])) 
{
$j = 0;     // Variable for indexing uploaded image.
$target_path = "uploads/";     // Declaring Path for uploaded images.
if(count($_FILES['file']['name']) > 0)  // to make sure their is items in the $_FILES['upload'] array
{
    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.
        $namePic=microtime_name(); 
        $target_path = $target_path . $namePic . "." . $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 (in_array($file_extension, $validextensions))
        {
            echo 'image is uploaded';
            //print_r($_FILES['file']['tmp_name']);
            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/>';
                $image_name=$target_path;
                $query=$conn->prepare("INSERT INTO pic (pic_id, pic_path)
                                VALUES ('', :pic_path)");
                $query->bindParam(':pic_path', $image_name);
                echo $image_name;
                $query->execute();
                //if($stmt)
                    echo 'success!';

            } 
            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/>';
        }
    }
}
}
?>
我的教授。我说当我上传它们时,没有分隔符作为文件之间的分隔符。我试图搜索,找不到任何解决方案来放置分隔符。请帮忙!

0 个答案:

没有答案