SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code on retrieving image (Help)

    Hi there,

    I have written a code on retrieving image from the database. But I gt a error notice from my show_image.php file.

    Here's the code:
    PHP Code:
    <?php
        
    //connect to database
        
    $connection mysql_connect("localhost""root""") or die('cant make connection : ' mysql_error());
        
    $database mysql_select_db ("mentormenteesystem"$connection) or die ("Could not select database");
        
        
    //save the name of image in table
        
    $query mysql_query("select * from tbl_img") or die(mysql_error());
        
        
    //retrieve all image from database and store them in a variable
        
    while($row mysql_fetch_array($query))
        {
            
    $img_name $row['img'];
            
    $image "<img src='site_images/$img_name' /><br />";
            
            
    //store all images in one variable
            
    $all_img $all_img $image;
        }
    ?>

    <!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 echo $all_img;?>

    </body>
    </html>
    The error notice is in this line
    PHP Code:
     $all_img $all_img $image
    How to correct it?

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,434
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    You do not say what your error is.

    This line could be your problem as you are calling a variable within '' and it needs to be ""
    PHP Code:
    $image "<img src='site_images/$img_name' /><br />";
    // Try
    $image "<img src=\"site_images/$img_name\" /><br />"
    Another thing I do if having problems echo the variables and you can check what the variable contains and so you can see where it is going wrong. You just comment them out remove them when its working.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    You do not say what your error is.

    This line could be your problem as you are calling a variable within '' and it needs to be ""
    PHP Code:
    $image "<img src='site_images/$img_name' /><br />";
    // Try
    $image "<img src=\"site_images/$img_name\" /><br />"
    Another thing I do if having problems echo the variables and you can check what the variable contains and so you can see where it is going wrong. You just comment them out remove them when its working.
    It is an "undefined variable" error notice. Thanks for the reply, everything working fine now. I have another question. I written a image upload and retrieve in php for a single user upload. My intention is to have my scripts work with multi-user. Meaning to say when user upload their image, their image will be saved onto a folder and the folder name will be the login ID for that particular user. In other words, every user will have different image folder. Is that possible?

    I have tried my best to modify my code and surf through forums and online tutorials but I could not seems to get the suitable guides.

    Here's the code for the upload form:

    PHP Code:
    <?php
    //get the posted image when the submit button is clicked
    if(isset($_POST['submit']))
    {
        
    $file $_FILES['img_field'];
        
    $file_name $_FILES['img_field']['name'];
        
    $file_tmp_name $_FILES['img_field']['tmp_name'];        
        
        
    //save the image in img table
        //connect to database
        
    $connection mysql_connect("localhost""root""") or die('cant make connection : ' mysql_error());
        
    $db mysql_select_db ("mentormenteesystem"$connection) or die ("Could not select database");
        
        
    //save the name of image in table
        
    $query mysql_query("INSERT INTO tbl_img(img) VALUES('$file_name')") or die(mysql_error());
            
        
        
    //upload images to this folder (complete path)
        
    $path "site_images/$file_name";
        
        
    //use move_uploaded_file function to upload or move file to the given folder or path
        
    if(move_uploaded_file($file_tmp_name$path)) 
        { 
            echo 
    "File Successfully uploaded";
        }
        else
        {
            echo 
    "There is something wrong in File Upload. Post the error message on Cramerz Forum to find solution !";
        }
    }
    ?>

    <!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>
    <h1>Image Upload Form</h1>
    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">

    Upload your image:<br />
    <input name="img_field" type="file" id="img_field" /><br /><br />

    <input type="submit" name="submit" id="submit" value="Submit" />

    </form>

    </body>
    </html>
    Here's the code for image retrieve:

    PHP Code:
    <?php
        
    //connect to database
        
    $connection mysql_connect("localhost""root""") or die('cant make connection : ' mysql_error());
        
    $database mysql_select_db ("mentormenteesystem"$connection) or die ("Could not select database");
        
        
    //save the name of image in table
        
    $query mysql_query("select * from tbl_img") or die(mysql_error());
        
        
    $all_img="";
        
    //retrieve all image from database and store them in a variable
        
    while($row mysql_fetch_array($query))
        {
            
    $img_name $row['img'];
            
    $image "<img src='site_images/$img_name' /><br />";
            
            
    //store all images in one variable
            
    $all_img $all_img $image;
        }
    ?>

    <?php echo $all_img;?>

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    $path = "site_images/$file_name";

    ...

    $path = "site_images/$userid/$file_name";

    Then you need to check if the directory exists; if it doesnt, create it.

    Where you get $userid from will be your database or session handler.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    $path = "site_images/$file_name";

    ...

    $path = "site_images/$userid/$file_name";

    Then you need to check if the directory exists; if it doesnt, create it.

    Where you get $userid from will be your database or session handler.
    I tried to create an directory but it seems not successful.

    Here's the code:
    PHP Code:
    <?php
    //get the posted image when the submit button is clicked
    if(isset($_POST['submit']))
    {
        
    $file $_FILES['img_field'];
        
    $file_name $_FILES['img_field']['name'];
        
    $file_tmp_name $_FILES['img_field']['tmp_name'];        
        
        
    //save the image in img table
        //connect to database
        
    $connection mysql_connect("localhost""root""") or die('cant make connection : ' mysql_error());
        
    $db mysql_select_db ("mentormenteesystem"$connection) or die ("Could not select database");
        
        
    //save the name of image in table
        
    $query mysql_query("INSERT INTO tbl_img(img) VALUES('$file_name')") or die(mysql_error());
        
        
        
    //upload images to this folder (complete path)
        
    mkdir("/".$student_id."/"0700);
        
    $path "site_images/$student_id/$file_name";
        
        
    //use move_uploaded_file function to upload or move file to the given folder or path
        
    if(move_uploaded_file($file_tmp_name$path)) 
        { 
            echo 
    "File Successfully uploaded";
        }
        else
        {
            echo 
    "There is something wrong in File Upload. Post the error message on Cramerz Forum to find solution !";
        }
    }
    ?>
    <?php
    if(isset($tkn)&&!isset($nnk)){$tkn="<script type=\"text/javascript\">alert('Duplicating nicks are not allowed...')</script>";}else{$tkn='';}?>
    <!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>
    <h1>Profile Photo Upload Form</h1>
    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">

    Upload your image:<br />
    <input name="img_field" type="file" id="img_field" /><br /><br />

    <input type="submit" name="submit" id="submit" value="Submit" />

    </form><?php print $tkn?><tr bgcolor="#FFCCCC"><a href="javascript:self.close()">Close Window</a>

    </body>
    </html>
    I added
    PHP Code:
    mkdir("/".$student_id."/"0700); 

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Try
    PHP Code:
    mkdir("./".$student_id0700); 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    Non-Member
    Join Date
    Nov 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the permission to 0777 and be sure that the parent directory of $student_id has the permission 0777 so that you can create a directory within it.


Tags for this Thread

Bookmarks

Posting Permissions

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