Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts

    Cool Image Hosting Website Link

    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?
    Last edited by angelali; 02-23-2012 at 05:50 PM.

  • #2
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Just write an if statement.

    For example:

    PHP Code:
    $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.
    Last edited by stevenmw; 02-23-2012 at 05:58 PM.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    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)

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Here are my codes:

    PHP Code:
    <?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($imagestrpos($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>';
    }
    }
    }
    ?>

  • #6
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    PHP Code:
     <?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($imagestrpos($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)
    Last edited by stevenmw; 02-23-2012 at 06:45 PM.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I got this...

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

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

  • #8
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Oops, I goofed it up. Try this:
    PHP Code:
    <?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($imagestrpos($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>';
    }
    }
    }
    ?>
    Last edited by stevenmw; 02-23-2012 at 07:16 PM.

  • #9
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    If that doesn't work try:

    PHP Code:
    <?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($imagestrpos($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>';
    }
    }
    }
    ?>

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I am getting the same.. look the link, upload an image you will see...the problem is the concatenation.

  • #11
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Quote Originally Posted by angelali View Post
    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

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

  • #12
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Does not work...the problem is the image.. even I tried it by putting them in a variable in vain. It sucks the concatenation..

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    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>';

    Thank you for helping me alot!

  • #14
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Great! looks good.

  • #15
    Regular Coder
    Join Date
    Sep 2011
    Posts
    366
    Thanks
    39
    Thanked 0 Times in 0 Posts
    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..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •