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 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts

    upload path to database

    Hi, i am struggling to get this part before i tackle the next part. i have a upload form i have created (very simple just a browse and a upload button) when you click upload this code is run (shown below) but that stores the file in the database as a file using blob but what i would really like to do is upload the images to a this folder (http://localhost/Blean_Photos/images) and have the path stored in the database. i havent a clue on how to change my code i have now to do that. the next part would be to display the images but i would like to tackle one part at a time

    thank you to anyone who could help

    Code:
    <?php
     // Check if a file has been uploaded
     if(isset($_FILES['uploaded_file'])) {
         // Make sure the file was sent without errors
         if($_FILES['uploaded_file']['error'] == 0) {
             // Connect to the database
             $dbLink = new mysqli('localhost', 'root', '', 'gallery');
             if(mysqli_connect_errno()) {
                 die("MySQL connection failed: ". mysqli_connect_error());
             }
      
             // Gather all required data
             $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
             $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
             $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
             $size = intval($_FILES['uploaded_file']['size']);
      
             // Create the SQL query
             $query = "
                 INSERT INTO `images` (
                     `name`, `mime`, `size`, `data`, `created`
                 )
                 VALUES (
                     '{$name}', '{$mime}', {$size}, '{$data}', NOW()
                 )";
      
             // Execute the query
             $result = $dbLink->query($query);
      
             // Check if it was successfull
             if($result) {
                 echo 'Success! Your file was successfully added!';
             }
             else {
                 echo 'Error! Failed to insert the file'
                    . "<pre>{$dbLink->error}</pre>";
             }
         }
         else {
             echo 'An error accured while the file was being uploaded. '
                . 'Error code: '. intval($_FILES['uploaded_file']['error']);
         }
      
         // Close the mysql connection
         $dbLink->close();
     }
     else {
         echo 'Error! A file was not sent!';
     }
      
     // Echo a link back to the main page
     echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
     ?>

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Refer to this: php file upload
    Basically, most of the code you provided above will become redundant

    PHP Code:
    <?php
     
    // Check if a file has been uploaded
     
    if(isset($_FILES['uploaded_file'])) {
         
    // Make sure the file was sent without errors
         
    if($_FILES['uploaded_file']['error'] == 0) {

         
    // @@@@ Edits Start here @@@@
         // Move the file to the path of your choice; follow example in the link provided earlier
         // now connect to DB and store the $target_path (from linked example) into the DB
         //   simply modify your existing code to perform this task
         // note that you may have to modify the DB
         // @@@@ Edits end here @@@@
         
     
    }
     else {
         echo 
    'Error! A file was not sent!';
     }
      
     
    // Echo a link back to the main page
     
    echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
     
    ?>
    To retrieve the image path, connect to DB, find the image you're looking for (hopefully, you have a unique identifier that segregates images from each other) and then display it using the returned result.

  • Users who have thanked shadowmaniac for this post:

    saunders1989 (01-13-2010)

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    this is what i tried changing it to but i am not very good at php could you explain where ive gone wrong please. also when you say i have a unique identifier could you elaborate on that please.

    thanks

    Code:
    if(isset($_FILES['uploaded_file'])) {
         // Make sure the file was sent without errors
         if($_FILES['uploaded_file']['error'] == 0) {
    		 
    		 $target_path = "http://localhost/Blean_Photos/images/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    	
    	$dbLink = new mysqli('localhost', 'root', '', 'gallery');
             if(mysqli_connect_errno()) {
                 die("MySQL connection failed: ". mysqli_connect_error());
    			 
    			 
    	$query = "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`)
                 VALUES ('{$name}', '{$mime}', {$size}, '{$data}', NOW())";
    			 
    
    		 
    	 }
    		 
     else {
         echo 'Error! A file was not sent!';
     }
      
     // Echo a link back to the main page
     echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
     ?>

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    See comments within code:
    Code:
    if(isset($_FILES['uploaded_file'])) {
         // Make sure the file was sent without errors
         if($_FILES['uploaded_file']['error'] == 0) {
    		 
    
    // @@@@@@@@@@@@@@
    // Path is wrong. You have to specify the path of the folder on the web server
    // Typically, it'd be something akin to "var/www/Blean_Photos/images"
    // Also make sure the folder exists and that you have write permissions for it
    		 $target_path = "http://localhost/Blean_Photos/images/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    // @@@@@@@@@@@@@@
    // this echo is not really necessary
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    	
    	$dbLink = new mysqli('localhost', 'root', '', 'gallery');
             if(mysqli_connect_errno()) {
                 die("MySQL connection failed: ". mysqli_connect_error());
             }  // You missed this closing curly brace here
    			 
    
    // @@@@@@@@@@@@@
    // To keep things simple, we'll keep your DB structure the same except for 1 thing:
    // add a field of type 'varchar' with 100 or so characters, in your 'images' table.
    // Name it something like "image_path".
    // I copied over your "old" code but note that I got rid of "data" (which was your image)
    // and added $image_path variable instead
    
             // Gather all required data
             $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
             $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
             $size = intval($_FILES['uploaded_file']['size']);
             $image_path = $dbLink->real_escape_string($target_path);
    
    // @@@@@@
    // Note changes to your query as well
    // Make sure you added the "image_path" field to the "images" table!!
    	$query = "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`)
                 VALUES ('{$name}', '{$mime}', {$size}, '', NOW(), '{$image_path}')";
    			 
    
    		 
    	 }
    		 
     else {
         echo 'Error! A file was not sent!';
     }
      
     // Echo a link back to the main page
     echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
     ?>
    That should do the trick. Note that code is untested.



    Quote Originally Posted by saunders1989 View Post
    also when you say i have a unique identifier could you elaborate on that please.
    Something like an SSN, Student ID, ... that allows you to identify uploaded images from each other. This will make things easier when you try to retrieve image paths from the DB.

  • Users who have thanked shadowmaniac for this post:

    saunders1989 (01-13-2010)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    thanks for that code theres a slight problem i cant understand how to fix it. im getting this error 'Warning: move_uploaded_file(http://localhost/Blean_Photos/images/invalid carriage.jpg) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in C:\wamp\www\Blean_Photos\add_file.php on line 25

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php5307.tmp' to 'http://localhost/Blean_Photos/images/invalid carriage.jpg' in C:\wamp\www\Blean_Photos\add_file.php on line 25' any ideas what that could be?

    for the id i have a incrementing id at the moment but i will also be having radio buttons for certain galleries and depending on which one is selected a id will be givin and uploaded to the database aswell. is that possible? if that is i would be able to use a for loop that ++ til it reaches the end and display all the images that have the id=1 or id=2

    thanks again youve been soo much help
    Last edited by saunders1989; 01-13-2010 at 11:42 AM.

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    // @@@@@@@@@@@@@@
    // Path is wrong. You have to specify the path of the folder on the web server
    // Typically, it'd be something akin to "var/www/Blean_Photos/images"
    // Also make sure the folder exists and that you have write permissions for it
    $target_path = "http://localhost/Blean_Photos/images/";
    See bolded. I didn't fix your target path in the code above as I didn't know what the structure of your web server looked like.
    It should be something like those shown below or a variation of them:
    PHP Code:
    $target_path "images/"
    OR
    PHP Code:
    $target_path "c:/wamp/www/Blean_Photos/images/"

    for the id i have a incrementing id at the moment but i will also be having radio buttons for certain galleries and depending on which one is selected a id will be givin and uploaded to the database aswell. is that possible? if that is i would be able to use a for loop that ++ til it reaches the end and display all the images that have the id=1 or id=2
    Yes it's possible. No, you don't really need a for loop as long as you have the appropriate SQL query, a while loop will do the trick.
    Look up some tutorials on PHP/MySQL pertaining to selecting data from the DB and printing them out. That should be all you need.
    Last edited by shadowmaniac; 01-13-2010 at 05:04 PM. Reason: Missed part of the question

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    okay cool thanks for that. its all uploading nicely and it gets picked up in my folder and writes to my database. but for some reason it wont let me upload images above 2mb. i have no idea why. i havent got any restrictions as i can see even on my form there is not a 'MAX_SIZE' so i dont know why it wont let me do it. any ideas?

    Thanks alot for your help!

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by saunders1989 View Post
    okay cool thanks for that. its all uploading nicely and it gets picked up in my folder and writes to my database. but for some reason it wont let me upload images above 2mb. i have no idea why. i havent got any restrictions as i can see even on my form there is not a 'MAX_SIZE' so i dont know why it wont let me do it. any ideas?

    Thanks alot for your help!
    Modify your php.ini file

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    ive changed it to this:

    ; Maximum allowed size for uploaded files.
    ; http://php.net/upload-max-filesize
    upload_max_filesize = 5M

    but it doesnt seem to work either. i also have 2 php.ini files. is there any other files i should change?

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Refer to the link I posted. You might have to increase execution time, ... as well.
    As for the php.ini, since you're using Wamp, on the windows taskbar, locate the wamp icon, left click on it, go to php and click on php.ini. That's the php.ini file you need to modify.

    Do remember to restart wamp afterwards!

  • Users who have thanked shadowmaniac for this post:

    saunders1989 (01-13-2010)

  • #11
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    it works now. thank you soo much for you help!!!

  • #12
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Saunders,

    Was wondering if you have moved onto your next part of project. Displaying images. I am a newbie to PHP and found this thread quite helpful


  •  

    Posting Permissions

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