简体   繁体   中英

My php upload script malfunctioning

This is my script :

<?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>

The above code is made to take the zip file from user and upload it to the directory "uploaded/". But it is not upload the file to the directory and the name,size and type displays are not working. They just show No file uploaded ! even after the upload. I dont know where I am going wrong. Please help me .
Thanks in advance !

You may use the following upload script to validate and upload zip files. ( 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> 

replace

move_uploaded_file($target_file,"uploaded/".$file_name);

with

move_uploaded_file($file_tmp,"uploaded/".$file_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM