Lykas
Lykas

Reputation: 117

PHP uploading files: failed to get error message

I'm trying to upload files through the PHP function move_uploaded_file and this is what I have so far:

if (move_uploaded_file($file["tmp_name"], iconv("UTF-8","big5",$target_file))) {
    return true;
} 
else {
    echo "Not uploaded because of error #".$file["error"];
    exit(0);
    return false;
}

As for $target_file, it is the location I'd like to upload my files, which is currently stu_feedback/105502504/feedback_20180910.pdf. Yet, the files are all failed to be uploaded, which obviously goes to the else part. But when I wanted to echo the error message, it only shows 0.

I use Filezilla Client. I have tested on my localhost and it could upload files correctly. Does anyone know what actually happened?

Upvotes: 0

Views: 104

Answers (1)

GPiter
GPiter

Reputation: 799

I see some problems on this part :

  1. Add enctype to the form <form enctype="multipart/form-data">;
  2. Be sure that the folder where you want to move files are permissions (chmod -R /folder_name 777)

  3. Try to put in a try catch you code something like :

    try{
        if (move_uploaded_file($file["tmp_name"], iconv("UTF- 8","big5",$target_file))) {
            return true;
        } 
        else {
          echo "Not uploaded because of error #".$file["error"];
          //exit(0);
          return false;
        }
     }catch (\Exception $e){
         var_dump($e->getMessage());
         die();
     }
    

Upvotes: 1

Related Questions