简体   繁体   中英

php - uploading file using doesn't give error

what i want to do is to upload image to the server using php

this is my code

<?php
try {
    $name = isset($_POST['variable2']);
    $file = rand(1000,100000)."-".isset($_FILES['file']['name']);
    $file_name = isset($_FILES['file1']['name']);
    $file_loc = isset($_FILES['file1']['tmp_name']);
    $file_size = isset($_FILES['file1']['size']);
    $file_type = isset($_FILES['file1']['type']);
    $folder="uploads123/";
    $new_size = $file_size/1024;  
    $new_file_name = strtolower($file);
    $final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
    {
        echo "good";
    }else{
        echo "error";
    }
} catch (PDOException $pe) {
                        die("Error occurred:" . $pe->getMessage());
                    }       

?>

these are my problems

  1. why is it that the output it just the "error" from the if statement? it doesn't give details about the error even if i'm using try and catch.
  2. even if i give a wrong foldername(the directory where the image to be uploaded) it just give me output like "error" from the if statement. it doesn't give error that the folder doesn't exist on the server.

thank you.

Your html form is like that?

<form action="target.php" enctype="multipart/form-data">
<!-- Content -->
</form>

编辑:尝试将PDOException更改为Exception

Does folder exists? check privilegies on server for folder

Good way before move file:

$upload_dir = '/uploads123';
if(!is_dir($upload_dir)){
    mkdir($upload_dir, 0777); // you may set your access rule
}

Then you can try move file to this dir

isset() returns a boolean (true or false)

$file_loc will not be a valid file location, but is a boolean:

$file_loc = isset($_FILES['file1']['tmp_name']); // $file_loc = true

move_uploaded_file wil fail as it has no valid file to move and returns false . Your code will echo "error" as a result of move_uploaded_file returning false .

There is no Exception thrown, let alone a PDOException.

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