1

I'm trying to upload files in php using the following function :

public function fileUpload($FILES){
    $num_of_uploads = 1;
    $max_file_size = 1048576; //can't be larger than 1 MB

    $T = array ();
    foreach($_FILES["file"]["error"] as $key=>$value){

        if($_FILES["file"]["name"][$key] != ""){

            if($value == UPLOAD_ERR_OK){

                $v = array ();

                $origfilename = $_FILES["file"]["name"][$key];
                $filename = explode(".", $_FILES["file"]["name"][$key]);
                $filenameext = $filename[count($filename) - 1];

                $v['name'] = $filename[0];
                $v['extension'] = $filename[1];
                $v['type'] = $_FILES["file"]["type"][$key];

                unset($filename[count($filename) - 1]);
                $filename = implode(".", $filename);
                $filename = "file__" . time() . "." . $filenameext;

                if($_FILES["file"]["size"][$key] < $max_file_size){
                    $v['content'] = file_get_contents($_FILES["file"]["tmp_name"][$key]);
                    $T[] = $v;
                }else{
                    throw new Exception($origfilename . " file size inaccepted!<br />");
                }

            }else{
                throw new Exception($origfilename . " Error of upload <br />");
            }
        }
    }
    return $T;
}

This function works great with txt types, but when I'm testing pdf, or gif or jpg, it returns a damaged file.

7
  • check you php.ini file for max file upload size and max post size. Ensure upload size is not greater than that Commented Jun 7, 2016 at 10:55
  • I have no probleme with file size, i'm testting with 70Ko file and it's not working Commented Jun 7, 2016 at 10:59
  • What error you are getting? Commented Jun 7, 2016 at 11:02
  • I have no error, but when I'm downloading it, the document is damaged Commented Jun 7, 2016 at 11:04
  • Try to access uploaded file directly from uploaded location. There is a chance that during file download you might not have set headers properly. And due to that document is not downloading/open properly. Commented Jun 7, 2016 at 11:14

2 Answers 2

3

As far as I know, file_get_contents() works well on text/html types. However, for other file types you should parse their text content first to use it in further processing. Try opening any .pdf in Notepad to see it's text content.

For uploading purposes, use move_uploaded_file() in your cycle, like this:

move_uploaded_file($_FILES["file"]["tmp_name"][$key], $filename);

Of course, without trying to get text content from uploaded file.

Sign up to request clarification or add additional context in comments.

2 Comments

move_uploaded_file function must have in second parameter the file destination, not the filename, move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src); Here I'm not saving files in a specific destination
Sure, but filename will work as destination for current working directory as well. If for some reason you want to use file content for direct download, you'll need to properly set Content-type headers then, depending on the current file extension.
1

For downloading the file, you need to set headers. So, at the starting of function try setting any of below header for png or jpeg files:

//For png file
header("Content-Type: image/png");
//For jpeg file
header("Content-Type: image/jpeg");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.