Hello, welcome to my first Php: main tutorial.
Before i start, i must say that not all of the code here is mine.
Before you setup this type of image host, think if you really want to use up all of that bandwidth/webspace.
Ok, to start make sure your host supports file upload and php.
If yes, continue:
First create a folder in your main directory and name it "imagehost" or something. In side that folder make another folder called "uploads", Your now ready.
In "imagehost" or whatever you named it, save 2 blank files: upload.html and takefile.php.
Open up upload.html and paste in this: (I'm not going to explain it)
<form enctype="multipart/form-data" action="takefile.php" method="post">
Choose an Image to upload:
<br />
<input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload Image" />
</form>
So, you now have your form ready, if you want you can open it now and you should see a file chooser and a submit button.
Now open up takefile.php and paste in this:
<?php
$upload_path = "uploads/";
$_FILES['uploadedfile']['name']=$_FILES['u
ploadedfile']['name'].'_'.time()+rand(1000
,9999);
$filetype = $_FILES['uploadedfile']['type'];
$max_filesize = 500000;
$upload_path = $upload_path . basename( $_FILES['uploadedfile']['name']);
if ($_FILES['uploadedfile']['size'] > $max_filesize) {
die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes.');
}
if ($filetype!="image/jpeg"&&$filetype!="imag
e/gif"&&$filetype!="image/png") {
die('Your file was not a jpeg, png or gif.');
}
if(move_uploaded_file($_FILES['uploadedfil
e']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
echo "<br><br>your file can be found at:<br>http://www.SITE-HERE..uploads/veiw.
php?id=" . basename($_FILES['uploadedfile']['name']);
} else{
echo "There was an error uploading the file, please try again!";
}
?>
I'll now explain the above code:
$target_path = "uploads/";
This sets the path to where the images should be uploaded.
$_FILES['uploadedfile']['name']=$_FILES['u
ploadedfile']['name'].'_'.time()+rand(1000
,9999);
This changes the name of the file being uploaded, just say i wanted to upload pic.jpg, it would be changed into something like 1124335454_pic.jpg. This is usefull because it keeps the images more secure, and they won't overwrite each other.
$filetype = $_FILES['uploadedfile']['type'];
This finds out the filetype of the file being uploaded, and sets it as the value of $filetype.
$max_filesize = 500000;
This is to set the maximum filesize that the users can upload, change it to whatever you wish.
$upload_path = $upload_path . basename( $_FILES['uploadedfile']['name']);
This sets what the image is going to be saved as/to. For example if my picture was pic.jpg $upload path would be changed from "uploads/" to "uploads/pic.jpg".
if ($_FILES['uploadedfile']['size'] > $max_filesize) {
die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes.');
}
This bit of code checks the size of the file, and if its above the value of $max_filesize, it cancels the upload and Prints out "Your filesize is too large. Please make your filesize smaller than $max_filesize".
if ($filetype!="image/jpeg"&&$filetype!="imag
e/gif"&&$filetype!="image/png") {
die('Your file was not a jpeg, png or gif.');
}
This code checks if the file size is either jpeg, gif or png. If it isn't the upload is canceled and it prints out "Your file was not a jpeg, png or gif.".
if(move_uploaded_file($_FILES['uploadedfil
e']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
echo "<br><br>your file can be found at:<br> http://www.SITE-HERE..uploads/veiw.php?id=
" . basename($_FILES['uploadedfile']['name']);
} else{
echo "There was an error uploading the file, please try again!";
}
This bit of code is the most inportant, it uploads the image to a temporary folder and then copies it to uploads/ when finished. It displays a link to the file aswell. Change "SITE-HERE.com" to your domain name.
If something goes wrong for example it can't find uploads/, it prints out "There was an error uploading the file, please try again!"
So, thats the code that gets the file onto the server, now how about veiwing it?
Go into the uploads folder and create a php file called "veiw". Paste into this php file:
<?php $id = $_GET['id']; ?>
<img src="<?php echo $id; ?>" border="1" />
Now i shall explain this little bit of code:
<?php $id = $_GET['id']; ?>
Where it says a link to this page in takefile.php at the end it has something like veiw.php?id=123543653_pic.jpg, what this code does it get that id and store it as the value of $id.
<img src="<?php echo $id; ?>" border="1"></img>
This pretty much just uses prints $id as the image source.
You (should) now have a working image host!
I'd recomend you have it on http://www.phpnet.us/ as its free hosting, that supports php, and gives a whopping 80gb bandwidth.
Enjoy your image host.
Please report any errors.
-Jordan