(PHP) simple picture/file upload script


Here is a simple tutorial script that uploads a file to a folder on the server. In this example it is assumed that the file is an image and it will be outputted on screen in an img-tag. But the file uploading mechanics works for any file type.

[php]
<?php
if(isset($_POST[‘submit’]))
{
$temp_name = $_FILES["file"]["tmp_name"]; // get the temporary filename/path on the server
$name = $_FILES["file"]["name"]; // get the filename of the actual file

// print the array (for reference)
print_r($_FILES);

// Create uploads folder if it doesn’t exist.
if (!file_exists("uploads")) {
mkdir("uploads", 0755);
chmod("uploads", 0755); // Set read and write permissions of folder, needed on some servers
}

// Move file from temp to uploads folder
move_uploaded_file($temp_name, "uploads/$name");
chmod("uploads/$name", 0644); // Set read and write permissions if file

}
?>

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="submit"/>
</form>

[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *