Reputation:
OK so I want to be able to upload a image to my webserver via PHP for the admin control panel. Now, I want to be able to upload the image by using a file uploader, and use a textbox for the filename the image will be called. Cheers :D
Upvotes: 0
Views: 70
Reputation: 706
use a standard form in html
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Filename: <input name="name" type="text" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
then the uploader.php file:
//get the data from the form
$target_path = "uploads/";
$name= $_POST['name'];
$target_path = $target_path . $name;
//this is the script which uploads the file
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded as" . $name;
} else{
echo "There was an error uploading the file, please try again!";
}
you might want to check the file is an image .. to do so:
$max_size=20000; //in kb
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < $max_size)
&& in_array($extension, $allowedExts))
{
//here goes the script to upload the file!!
}else{
echo"The file is not supported";}
Upvotes: 1