...

View Full Version : Image Hosting Website Link



angelali
02-23-2012, 05:43 PM
Hello guys, I have created a mini image hosting website. Well, I have successfully coded the file upload, including security to allow certain image extensions and size as a beginner in PHP.

However, only one thing remains is the image link. You can view the website on this address http://mini-image-hosting.99k.org/ where it is currently hosting on a free web hosting account with a free sub-domain.

Right now, only the image can be uploaded and is being stored in a directory. Nevertheless, I want that when the person uploads an image, he gets also the link, for example: http://mini-image-hosting.99k.org/xxx.jpg something like that.

Can you help me for this?

stevenmw
02-23-2012, 05:56 PM
Just write an if statement.

For example:



$filename = $_POST['uploadinputfield'];

if (your image upload success code) {
echo "http://www.yoursite.com/"
echo $filename;
}
else {
your file did not upload successfully
};
Get the idea?

It shouldn't be too hard to incorporate. I used your upload form, and it already tells me the name of my file whenever I upload. Just echo the url before the filename in your code.

angelali
02-23-2012, 06:37 PM
Huhh, I don't think I have understand. I mean once the image has successfully uploaded, the person gets the link of the image as well. For example, the link is assigned automatically. For example, suppose you are uploading an image called xxx.jpg, once successfully uploaded, it gives also the link like mini-image-hosting/xxx.jpg.

stevenmw
02-23-2012, 06:39 PM
Exactly..
Look in your code. It echos the uploaded file name each time a file is uploaded. All you have to do is repeat that code, but put url in front of it.

Post your code, and I'll show you. (the php code for the file that uploads the image)

angelali
02-23-2012, 06:40 PM
Here are my codes:


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

//Assigning variables
$image = $_FILES['fileupload']['name'];
$size = $_FILES['fileupload']['size'];
$format = $_FILES['fileupload']['type'];
$tmp_name = $_FILES['fileupload']['tmp_name'];

//Check extensions and size
$fileextension = strtolower(substr($image, strpos($image, '.') + 1));
$max = 204800;

//Codes
if (isset($image)) {
if (empty($image)) {
echo '<p class="error">Choose an image to upload!</p>';
} elseif ($fileextension !== 'jpg' && $fileextension !== 'jpeg' && $fileextension !== 'gif' && $fileextension !== 'png'){
echo '<p class="error">Only JPG, GIF and PNG are acceptable!</p>';
} elseif ($size > $max) {
echo '<p class="error">Image must not exceed 200KB!</p>';
} elseif (file_exists("upload/" .$image)) {
echo '<p class="error">'.$image.' '.'already exists, choose another image or rename it to another name!'.'</p>';
} else {
$location = 'upload/';
move_uploaded_file($tmp_name, $location.$image);
echo '<p class="success">Upload successful</p>';
echo '<p class="details">'.$image.'</p>';
}
}
}
?>

stevenmw
02-23-2012, 06:43 PM
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

//Assigning variables
$image = $_FILES['fileupload']['name'];
$size = $_FILES['fileupload']['size'];
$format = $_FILES['fileupload']['type'];
$tmp_name = $_FILES['fileupload']['tmp_name'];

//Check extensions and size
$fileextension = strtolower(substr($image, strpos($image, '.') + 1));
$max = 204800;

//Codes
if (isset($image)) {
if (empty($image)) {
echo '<p class="error">Choose an image to upload!</p>';
} elseif ($fileextension !== 'jpg' && $fileextension !== 'jpeg' && $fileextension !== 'gif' && $fileextension !== 'png'){
echo '<p class="error">Only JPG, GIF and PNG are acceptable!</p>';
} elseif ($size > $max) {
echo '<p class="error">Image must not exceed 200KB!</p>';
} elseif (file_exists("upload/" .$image)) {
echo '<p class="error">'.$image.' '.'already exists, choose another image or rename it to another name!'.'</p>';
} else {
$location = 'image/';
move_uploaded_file($tmp_name, $location.$image);
echo '<p><a href=\"http://www.yoursite.com/imagedirectory\">$image</a></p>';
echo '<p class="success">Upload successful</p>';
echo '<p class="details">'.$image.'</p>';
}
}
}
?>
Look at the third echo from the bottom. (don't forget to style it)

angelali
02-23-2012, 07:07 PM
I got this...

http://mini-image-hosting.99k.org/upload\$image

I think it the backslash should be forward slash right after the 'upload'?

stevenmw
02-23-2012, 07:14 PM
Oops, I goofed it up. Try this:


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

//Assigning variables
$image = $_FILES['fileupload']['name'];
$size = $_FILES['fileupload']['size'];
$format = $_FILES['fileupload']['type'];
$tmp_name = $_FILES['fileupload']['tmp_name'];

//Check extensions and size
$fileextension = strtolower(substr($image, strpos($image, '.') + 1));
$max = 204800;

//Codes
if (isset($image)) {
if (empty($image)) {
echo '<p class="error">Choose an image to upload!</p>';
} elseif ($fileextension !== 'jpg' && $fileextension !== 'jpeg' && $fileextension !== 'gif' && $fileextension !== 'png'){
echo '<p class="error">Only JPG, GIF and PNG are acceptable!</p>';
} elseif ($size > $max) {
echo '<p class="error">Image must not exceed 200KB!</p>';
} elseif (file_exists("upload/" .$image)) {
echo '<p class="error">'.$image.' '.'already exists, choose another image or rename it to another name!'.'</p>';
} else {
$location = 'image/';
move_uploaded_file($tmp_name, $location.$image);
echo '<p><a href=\"http://www.yoursite.com/imagedirectory/$image\">$image</a></p>';
echo '<p class="success">Upload successful</p>';
echo '<p class="details">'.$image.'</p>';
}
}
}
?>

stevenmw
02-23-2012, 07:15 PM
If that doesn't work try:



<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

//Assigning variables
$image = $_FILES['fileupload']['name'];
$size = $_FILES['fileupload']['size'];
$format = $_FILES['fileupload']['type'];
$tmp_name = $_FILES['fileupload']['tmp_name'];

//Check extensions and size
$fileextension = strtolower(substr($image, strpos($image, '.') + 1));
$max = 204800;

//Codes
if (isset($image)) {
if (empty($image)) {
echo '<p class="error">Choose an image to upload!</p>';
} elseif ($fileextension !== 'jpg' && $fileextension !== 'jpeg' && $fileextension !== 'gif' && $fileextension !== 'png'){
echo '<p class="error">Only JPG, GIF and PNG are acceptable!</p>';
} elseif ($size > $max) {
echo '<p class="error">Image must not exceed 200KB!</p>';
} elseif (file_exists("upload/" .$image)) {
echo '<p class="error">'.$image.' '.'already exists, choose another image or rename it to another name!'.'</p>';
} else {
$location = 'image/';
move_uploaded_file($tmp_name, $location.$image);
echo '<p><a href=\"http://www.yoursite.com/imagedirectory/';
echo $image;
echo '">$image</a></p>';
echo '<p class="success">Upload successful</p>';
echo '<p class="details">'.$image.'</p>';
}
}
}
?>

angelali
02-23-2012, 07:16 PM
I am getting the same.. look the link, upload an image you will see...the problem is the concatenation.

stevenmw
02-23-2012, 07:19 PM
I am getting the same.. look the link, upload an image you will see...the problem is the concatenation.

Sorry, I did it again... try



echo '<a href=\"http://www.ttt.com/directory/';
echo $image;
echo '\">';
echo $image;
echo '</a>';

angelali
02-23-2012, 07:30 PM
Does not work...the problem is the image.. even I tried it by putting them in a variable in vain. It sucks the concatenation.. :(

angelali
02-23-2012, 07:37 PM
Now it is working, I removed the >a href tag.. I did it

echo '<p class="success">http://www.ttt.com/directory/';
echo $image.'</p>';

;):rolleyes::thumbsup::) Thank you for helping me alot!

stevenmw
02-23-2012, 08:02 PM
Great! looks good.

angelali
02-23-2012, 08:16 PM
By the way try it, upload a pic etc..test the security and the result.. You cannot upload files which are more than 200kb, other than jpg, jpeg, png and gif and also same image name... well im new in php, so i tried it as a first web application.. well not first, its my 4th one, but..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum