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 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    45
    Thanks
    16
    Thanked 0 Times in 0 Posts

    generate url for the image

    iam doing my project image hosting website.i have php codings for uploading images.but i dont know how to automaticaly generate url for each uploading images.please help me to do generate link(url) for each uploading images in php
    Last edited by urex; 02-02-2012 at 11:13 AM.

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,378
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    This really depends on several things.

    1) Are you storing the images in a directory somewhere? Or directly in the database?
    2) Do you need to keep the images assigned to a particular user, or some sort of gallery number?
    3) Do you need to keep the images the same filename as uploaded, or change that too.
    4) Does the person uploading need to log-in?
    5) Can the person uploading submit multiple images in one upload?
    6) Are you using a MySQL database?
    7) Do you need to generate a thumbnail version of each image?

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,233
    Thanks
    51
    Thanked 100 Times in 99 Posts
    This isn't enough information to help. Answer some of the questions that Mlseim asked and we can better help, or provide coding.

    I have a feeling you mean after an upload, a link would appear to view the image.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    45
    Thanks
    16
    Thanked 0 Times in 0 Posts

    generate url for the image


    iam doing my project image hosting website.i have php codings for uploading images.but i dont know how to automaticaly generate url for each uploading images.please help me to do generate link(url) for each uploading images in php


    storing the images directly in the database,
    i need to keep the images assigned to a particular user,
    change the file name too,
    the person uploading need to log-in,
    the person can uploading submit multiple images in one upload,
    iam using a MySQL database,
    i need to generate a thumbnail version of each image.

  • #5
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,378
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Now I'll mention this again ... because I'm not sure if you understood what I was saying ...
    You can store images in a directory (pretend you call it "my_images"), and the filename
    (only the filename) is saved in a column of your MySQL table.

    OR

    You can store the actual image (the binary bytes and bits) into a MySQL table.
    There is no directory involved, and the filename is part of the table's record, along with filetype, etc.
    There is no URL to the image ... only a PHP script can render the image to a user's browser.

    =============

    You said that you're storing the images directly in the database,
    so the image file does not exist in a directory. You have to
    query the database in order to view the image. That means the
    URL link is to a PHP script, not to an image.

    You also say you will generate a thumbnail. That too needs to go
    into the database as well, in a different table column. Same thing ...
    you query the database to view the image.

    So your URL to see the image would look like this ...

    mysite.com/view.php?id=234

    A PHP script called "view" would query the table for id=234 and render
    that image to the user's browser. Only a PHP script can access the image,
    since you are storing it inside your MySQL database.

    And you said you already have a script to upload the images to your database.
    Can you elaborate on what the table and column names are for those images?

    How much of this "image hosting" project do you already have finished?


    .
    Last edited by mlseim; 02-02-2012 at 06:22 PM.

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    45
    Thanks
    16
    Thanked 0 Times in 0 Posts

    generate url for the image

    now only i started my project.leave all above things.just give me coding for url generation on uplaoding images.uploading in directory only .no need of mysql database,no need to directly store in user's account

    <?php
    if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];

    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
    if($mime_type == $type) {
    $okay = true;
    break;
    }
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
    $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }

    $target_path = "/home/imagehos/public_html/project/images/".$filename; // change this to the correct site path
    if(move_uploaded_file($source, $target_path)) {
    $zip = new ZipArchive();
    $x = $zip->open($target_path);
    if ($x === true) {
    $zip->extractTo("/home/imagehos/public_html/project/images/"); // change this to the correct site path
    $zip->close();

    unlink($target_path);
    }
    $message = "Your .zip file was uploaded and unpacked.";
    } else {
    $message = "There was a problem with the upload. Please try again.";
    }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php if($message) echo "<p>$message</p>"; ?>
    <form enctype="multipart/form-data" method="post" action="">
    <label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
    <br />
    <input type="submit" name="submit" value="Upload" />
    </form>
    </body>
    </html>\




    this is zip upload script.i have to generate url for each image

  • #7
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,378
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    I see ...

    Now you aren't using MySQL, which is OK .. you are storing the images in a directory.

    BUT ...

    How will you know who uploaded it?


    Anyhow ... this would be how to scan your images directory and create a link to each one ...
    PHP Code:
    <?php

    // folder name where they are stored.
    $folder_name="image";

    // put these types of images into an array.
    $images array_merge(
    glob("$folder_name/*.jpg"),
    glob("$folder_name/*.png"),
    glob("$folder_name/*.gif")
    );

    foreach(
    $images as &$img){
    echo 
    "<a href='$folder_name/$img'>$img</a><br />";
    }
      
    ?>

    .
    Last edited by mlseim; 02-03-2012 at 12:57 PM.

  • Users who have thanked mlseim for this post:

    urex (02-10-2012)


  •  

    Posting Permissions

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