PHP上传多个图片

时间:2016-02-20 04:59:36

标签: php directory image-uploading directory-structure

此代码以前工作,现在我无法弄清楚为什么它不会上传,我没有收到错误,我也没有收到任何回声或var_dumps,它就像按钮只刷新一样这页纸。 (只是为了澄清,有很多代码做了很多东西,但这是我的问题的原因,因为我将它隔离到另一个带有下面代码的项目,这给了我相同的结果)。

所有要做的就是创建一个由“ItemName”命名的文件夹,然后它应该将图像移动到新命名的文件夹中。

先谢谢你,这个问题一直阻碍了我几天......

HTML PAGE

<form id="newsell" enctype="multipart/form-data" method="post">

<input type="text" class="css-input" name="ItemName"  value="">

<input name="file[]" type="file" id="file" multiple />

<input type="submit" name="Upload" class="css-input1" value="Upload">

<?php
 if ($_POST['Upload']) {
 require_once("random.php"); 

}
?>

random.php

$MyLocation = "MyName";  // this comes from db, for this case just hardcode
$ItemName1 = htmlspecialchars($_POST["ItemName");
$ItemName = strip_tags($ItemName1);
$parentDir = "C:/wamp/www/HOME/uploadimages/".$MyLocation;   
echo "Does it exist...." . $parentDir . "/" . $ItemName;
if(!is_dir($parentDir)) { // Check if the parent directory is a directory
echo "Apologies, something has gone wrong.";
RandError();   // POPUP
die();
}

    if(!is_writable($parentDir)) { // Check if the parent directory is writeable
 echo "Apologies, something has gone wrong.";
        RandError();   // POPUP
        die();
    }
    if(mkdir($parentDir . "/" . $ItemName) === false) { // Create the directory
       echo "File apparently exists...." . $parentDir . "/" . $ItemName;
        ExistingSaleName();  // POPUP
        die();
    }
//  die('Created directory successfully'); // Success point
echo "AFTER INSERTION";
    movefiles();
}



function movefiles() {
$MyLocation = "MyName";
echo "In movefiles";
$ItemName1 = htmlspecialchars($_POST["ItemName"]);
$ItemName = strip_tags($ItemName1);
extract($_POST);
if (extract($_POST) === null) { // trying to fault find here, but never returns anyway due to some kind of bug as at one point it was returning a null value
echo "PROBLEM...";
}
$error=array();
$extension=array("jpeg","jpg","png");
$res = ("C:/wamp/www/HOME/uploadimages/". $MyLocation. "/" .  $ItemName);
foreach($_FILES["file"]["tmp_name"] as $key=>$tmp_name) {
    $file_name=$_FILES["file"]["name"][$key];
    $file_tmp=$_FILES["file"]["tmp_name"][$key];
    if (!(($_FILES["file"]["type"][$key] == "image/png") || ($_FILES["file"]    ["type"][$key] == "image/jpeg") || ($_FILES["file"]["type"][$key] ==     "image/jpg"))) {
        die("Only the .jpg / .jpeg / .png file's were uploaded.");
    } else {
        echo "SHIT";
    }
var_dump($file_tmp);
   $ext=pathinfo($file_name,PATHINFO_EXTENSION);
    $count;
    //check if file exist       
        if (!file_exists($res . "/" . $file_name)) {
            sleep(2);           
            if (isset($_FILES["file"]["tmp_name"][$key])) {
                move_uploaded_file($_FILES["file"]["tmp_name"][$key], $res);
                ++$count;
                if ($count >=5) {
                    // go_to(); // This goes onto the next function
                    die ("First 5 images are uploaded, <br/> 5 images     maximum.");
                }
            } else {
                echo "It exited HERE...";
            }
        } else {
            ExistingSaleName();
             die();
        }
}   
}

1 个答案:

答案 0 :(得分:0)

我创建了简单的代码来上传多个图片。改变你的。

<?php
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['files']['name'][$key];
    $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];  
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }       
    $query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
    $desired_dir="user_data";
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir("$desired_dir", 0700);        // Create directory if it does not exist
        }
        if(is_dir("$desired_dir/".$file_name)==false){
            move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
        }else{                                  // rename the file if another one exist
            $new_dir="$desired_dir/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }
     mysql_query($query);           
    }else{
            print_r($errors);
    }
}
if(empty($error)){
    echo "Success";
}

} ?&GT;

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
<input type="submit"/>