我的php上传脚本出现故障

时间:2015-11-15 11:51:58

标签: php html file-upload php-5.6

这是我的剧本:

<?php
 $target_dir = "uploaded/";
 $target_file = $target_dir . basename($_FILES["image"]["name"]);
 if(isset($_FILES['image'])){
  $errors= array();
  $file_name = $_FILES['image']['name'];
 <?php
 $target_dir = "uploaded/";
 $target_file = $target_dir . basename($_FILES["image"]["name"]);
if(isset($_FILES['image'])){
  $errors= array();
  $file_name = $_FILES['image']['name'];
  $file_size =$_FILES['image']['size'];
  $file_tmp =$_FILES['image']['tmp_name'];
  $file_type=$_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

  $expensions= array("zip");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a ZIP file.";
  }

  if($file_size > 2097152){
     $errors[]='File size is more than 2mb . Please email files to files.zipcracker.netne.net';
    }

  if(empty($errors)==true){
     move_uploaded_file($target_file,"uploaded/".$file_name);
     echo "Success";
     }
     else{
     print_r($errors);
     }
     }
      ?>      
     <form action="post.php" method="POST" enctype="multipart/form-data">
     <input type="file" name="image" /><br>
     <input type="submit">

     </form>

     <ul>
        <li>Sent file: <?php if (isset($_FILES['image']['name'])){ 
        echo $_FILES['image']['name']; } 
        else { 
        echo 'No file uploaded !';} 
        ?>
        <li>File size: <?php if (isset($_FILES['image']['size'])) { 
        echo $_FILES['image']['size']; }
        else { echo 'No file uploaded !';} 
        ?>
        <li>File type: <?php if (isset($_FILES['image']['type'])) {
        echo $_FILES['image']['type'] ;} 
            else {
                echo 'No file uploaded !' ;}
                ?>
     </ul>

上面的代码是从用户那里获取zip文件并将其上传到目录&#34;上传/&#34;。但它不会将文件上传到目录,并且名称,大小和类型显示不起作用。他们只显示没有上传的文件!甚至在上传之后。我不知道我哪里错了。请帮我 。
提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以使用以下上传脚本来验证和上传zip个文件。 (tested

<?php
$upload_errors = array(
    UPLOAD_ERR_OK => "No errors.",
    UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
    UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE,",
    UPLOAD_ERR_PARTIAL => "Partial upload.",
    UPLOAD_ERR_NO_FILE => "No file.",
    UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
    UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
    UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
$custom_errors = array();

if(isset($_POST['submit'])){

    // process the form

    // 1. get the temporary uploaded file by its tmp_name
    $tmp_file = $_FILES['file_upload']['tmp_name'];

    // 2. File name for the destination. It could be anything based on our preference
    $target_file = basename($_FILES['file_upload']['name']);

    // 3. Specify the upload directory (It's actually a relative link)
    $upload_dir = "uploads";

    // 4. Check extension
    $extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
    if($extension != "zip"){
        $custom_errors[] = "extension not allowed, please choose a ZIP file.";
    }

    // 5. Check size
    if($_FILES['file_upload']['size'] > 2097152){
        $custom_errors[] = "File size is more than 2mb . Please email files to files.zipcracker.netne.net";
    }

    // 6. Check if there are no errors
    if(empty($custom_errors)){
        // 7. Now you actually move the file but before that you may use file_exists() to make sure there
        // isn't already a file by the same name. And again, move_uploaded_file will return false if 
        // $tmp_file is not a valid upload file or if it can't be uploaded for any other reason. In place
        // of DIRECTORY_SEPARATOR, you can use "/"
        if(move_uploaded_file($tmp_file, $upload_dir.DIRECTORY_SEPARATOR.$target_file)){
            // file uploaded successfully
        }else{
            // error
            echo $upload_errors[$_FILES['file_upload']['error']] . "<br />";
        }
    }else{
        // error
        foreach($custom_errors as $err){
            echo $err . "<br />";
        }
    }
}
?>      

 <!--Your HTML form-->
 <form action="post.php" method="POST" enctype="multipart/form-data">
     <input type="file" name="file_upload" /><br>
     <input type="submit" name="submit" value="Upload">
 </form>

 <!--Output-->
 <ul>
    <li>Sent file: 
        <?php 
        if (isset($_FILES['file_upload']['name'])){ 
            echo $_FILES['file_upload']['name']; } 
        else { 
            echo 'No file uploaded !';
        } 
        ?>
    </li>
    <li>File size: 
        <?php 
        if (isset($_FILES['file_upload']['size'])) { 
            echo $_FILES['file_upload']['size']; }
        else { 
            echo 'No file uploaded !';
        } 
        ?>
    </li>
    <li>File type: 
        <?php 
        if (isset($_FILES['file_upload']['type'])) {
            echo $_FILES['file_upload']['type'] ;} 
        else {
            echo 'No file uploaded !' ;
        }
        ?>
    </li>
 </ul> 

答案 1 :(得分:0)

替换

move_uploaded_file($ TARGET_FILE,“上传/".$ FILE_NAME);

move_uploaded_file($ file_tmp,“上传/".$ FILE_NAME);