Home php Loading Pictures on Server

Loading Pictures on Server

Author

Date

Category

Nowhere can I find an example of loading the picture to the server not from the form, but from the post request. Help remake this example with php.net:

& lt;? php
// PHP 4.1.0 and earlier versions should be used $ http_post_files
// instead of $ _files.
$ uploaddir = '/ VAR / WWW / Uploads /';
$ UploadFile = $ uploaddir. Basename ($ _ files ['userfile'] ['name']);
Echo '& lt; pre & gt;';
if (Move_uploaded_File ($ _ files ['userfile'] ['TMP_NAME'], $ uploadfile)) {
  Echo "The file is correct and was successfully loaded. \ n";
} else {
  Echo "Possible attack using the file load! \ n";
}
Echo 'Some debug information:';
Print_R ($ _ Files);
Print "& lt; / pre & gt;";
? & gt;

Answer 1, Authority 100%

Form is a set of named data, POST / GET / PUT / DELETE / REQUEST – data transfer methods. The transfer mainly goes through the query body (your post), or request parameters (GET – the file cannot be transmitted through it). The form only helps you conveniently upload the file through the POST method to the server. You need to be sure to read about The transmission of parameters from outside

The file download is correct. Transfer of a file from the form / request will be carried out by the POST method. In this example, there are problems, for example, no is_uploaded_file function, which checks that the file is loaded or not check, which is loaded exactly the picture, in the code below implemented this check.

& lt;? php
$ uploaddir = '/ VAR / WWW / Uploads /';
$ UploadFile = $ uploaddir. Basename ($ _ files ['userfile'] ['name']);
if (! is_uploaded_file ($ _ files ['userfile'] ['TMP_NAME']))
{
   ECHO "Loading a file to the server failed";
   DIE (); // or Throw Exception ...
}
// Check what's picture
if (! GetImagesize ($ _ files ["userfile"] ["tmp_name"])) {
   Echo "This is not a picture ...";
   DIE (); // or Throw Exception ...
}
if (Move_uploaded_File ($ _ files ['userfile'] ['TMP_NAME'], $ uploadfile)) {
  Echo "The file is correct and was successfully loaded. \ n";
} else {
  Echo "Possible attack using the file load! \ n";
}
? & gt;

To transfer data to the script you need to create a form on HTML with Encype = “Multipart / Form-Data”

& lt;! doctype HTML & GT;
& lt; html & gt;
& lt; Body & gt;
& lt; Form Action = "Upload.php" Method = "POST" Encype = "Multipart / Form-Data" & gt;
  Select Image to Upload:
  & lt; Input Type = "File" name = "userfile" id = "filetoupload" & gt;
  & lt; input type = "submit" Value = "upload image" name = "submit" & gt;
& lt; / form & gt;
& lt; / body & gt;
& lt; / html & gt;

If you want to send data without a browser, you can use CURL. Run a post request from the command line to transfer the file like this:

CURL -I -X POST -H "CONTENT-TYPE: MULTIPART / FORM-DATA"
-F "[email protected]" http: // myserver / upload

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions