Upload images by PHP

Somehow it depends to the basics of PHP, but there are a lot of beginners who are searching for a possibility to create an upload of images for there websites. So I will give a short explanation how to do this.First you need a HTML form with the correct enctype for uploading other data then text. In this form you have to add a input field from type file to select the file you want to upload.

<form name="kontakt" enctype="multipart/form-data" method="POST" action="action.php">
  <input name="foto" type="file" />
  <input type="submit" value="upload picture" />

When the submit button is now pressed, the binary data of the picture is submitted to the action.php script where you have to work with this data now.

If you want to save the file somewhere on the server, you have to create a picture from the binary data again. All information you need are stored in the assotiative $_FILES array from the HTTP POST request.

This array can include the data of 1-n files. For every file it stores the name, the filetype, the temporary name (where the binary data is stored in webspace system), the error code and the filesize.

In our case (code above) it includes the data for one file. This data must be stored now on your system permanently. This can be done with a single simple line of code:

move_uploaded_file($_FILES['foto']['tmp_name'], 'path/to/store/'.$_FILES['foto']['name']);

That’s all. This line moves the temporary stored data to the path you want and gives the file the original name.

You can also give the file a new name, if you want. This can be done with the same line of code:

move_uploaded_file($_FILES['foto']['tmp_name'], 'path/to/store/newname.jpg');

But for the version above you must know, which filetype will be uploaded. To make this a little bit more dynamic, you can read out the filetype from the $_FILES array, but then you have to cast the stored filetype to a file extention or you can read the extention directly from the file name:

cast filetype: (This function you have to call with the $_FILES[‚foto‘][‚type‘] information.)

$ext = getExtention($_FILES['foto']['type']);

function getExtention(&$filetype) {
  switch ($filetype) {
    case "image/gif":
      return "gif";
    case "image/jpeg":
      return "jpg";
      return: "txt";

get extention from filename:

# get filename from $_FILES array
$filename = $_FILES['foto']['name'];
# read index of position of dot
$pos = strrpos( $filename, "." );
# get substring from filename, begin
# at position of dot-index plus one
$ext = substr( $filename, $pos + 1);

Then the extention of the file is stored in the $ext variable and can be used in the line of code to store the file on you webspace system permanently:

move_uploaded_file($_FILES['foto']['tmp_name'], 'path/to/store/newname.'.$ext);

To make now everything complete, it is maybe necessary to change the rights on this file that is able to use it. Normaly it should have the rights 644 or 755. This can be done the also one line of code:

chmod ("path/to/store/newname.".$ext, 0755);

now you are able to do with the file what ever you want to do. 🙂



Print Friendly, PDF & Email

Leave a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht.