www.webdeveloper.com
Page 7 of 23 FirstFirst ... 5678917 ... LastLast
Results 91 to 105 of 340

Thread: How to: Upload images using PHP

  1. #91
    Join Date
    Mar 2007
    Posts
    7
    Thanks for that quick reply. I have settings on the uploaded_files folder at 777. When I don't add the sql code, everything works. This error only occurs when I add the sql code shown above.

  2. #92
    Join Date
    Feb 2007
    Posts
    10

    image filename

    Hi Bokeh

    Was wondering if you could help me?

    I have been referring to your image upload article/tutorial.

    This part of code is where you give the file a unique name before it is uploaded to the directory. You used a $now variable to get the time and date in your article i believe.

    PHP Code:
    $counter 1;
    WHILE (
    file_exists($uploadFilename $uploadsDirectory.$counter.$_FILES[$fieldname]['name'])){
        
    $counter++;
        }
        
    //$uploadFilename = $uploadsDirectory.$counter.$_FILES[$fieldname]['name']; 
    I have created a counter to add to each filename when uploaded so each image i upload for example appear slike "photo1" "photo2" and so on...

    The code above adds the counter variable to the beginning of the uploaded filename, which looks like "1photo" "2photo" when uploaded, but is there anyway of adding the counter variable to the end of the filename variable. I have tried to alter the code many times but have been unsuccessful so far. All i seem to get is a parse error when i move the .$counter. to the end of the line.

    Any ideas, i would appriciate it

    Thanks

  3. #93
    Join Date
    May 2004
    Location
    The Big M
    Posts
    669
    It's because the filename also includes the file extension. So you're basically making a picture.jpg2 which is creating a parse error. What you may have to do is split the filename by "." and insert a $counter."." in between the two resulting strings.

  4. #94
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by theuedimaster
    It's because the filename also includes the file extension. So you're basically making a picture.jpg2 which is creating a parse error. What you may have to do is split the filename by "." and insert a $counter."." in between the two resulting strings.
    If you want to retain the filename but check there is no collission you could do something like this:
    PHP Code:
    $FieldName 'images';
    $UploadsFolder 'uploads/';
    $output '';
    $MaxAttempts 50;

    foreach(
    $_FILES[$FieldName]['error'] as $k => $v)
    {
        if(
    === $v)
        {
            if(
    file_exists($Filename $UploadsFolder $_FILES[$FieldName]['name'][$k]))
            while(
    $MaxAttempts > ++$v and file_exists(
                
    $Filename=$UploadsFolder.preg_replace('/(?=\.[^.]+$)/','-'.$v,$_FILES[$FieldName]['name'][$k])
            ));
            
    $MaxAttempts $v or die('Please rename ' $_FILES[$FieldName]['name'][$k]);
            if(
    move_uploaded_file($_FILES[$FieldName]['tmp_name'][$k], $Filename))
            {
                
    $output .= "<p><em>{$_FILES[$FieldName]['name'][$k]}</em> saved OK!</p>\n";
            }
        }
    }
    $output or $output '<p>No files were saved</p>';
    echo 
    $output
    Last edited by bokeh; 03-06-2007 at 07:44 AM.

  5. #95
    Join Date
    Jan 2007
    Posts
    3
    i just added this in the upload.processor page after the upload code, right before redirecting to .sucess. works fine for me. $year and $month are variable i added to the upload.form page.


    //====================================
    $title=$now.'-'.$_FILES[$fieldname]['name'];

    mysql_select_db('myDB');

    $query=("INSERT INTO pix (title, year, month) VALUES ('$title', '$year', '$month')");
    //=====================================
    Last edited by Muncher; 03-06-2007 at 11:12 AM.

  6. #96
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by Muncher
    $title=$now.'-'.$_FILES[$fieldname]['name'];
    I thought you wanted to retain something that resembled the filename.

  7. #97
    Join Date
    Feb 2007
    Posts
    10
    Yeh thats correct, i need the original filename of the image that is being uploaded with a counter variable being added onto the end of the filename not before as the post above your last states

    example

    filename = photo.jpg

    new filename = photo1.jpg and so on...

    basically all the images i upload are ging to be called photo but i need a way of adding them so they are not duplicated so they are all individual, which makes it an easy way for me to know how many images i have uploaded


    photo156 being the 156th image i have uploaded kinda thing lol

  8. #98
    Join Date
    Feb 2007
    Posts
    10
    In response to the coment made by theuedimaster

    i modified the code in order to adopt your possible solution but stll had a parse error as follows.....

    Parse error: parse error, expecting `']'' in

    this is my modified code but i am not sure if i have entered it in the correct position as you mentioned???

    PHP Code:

    $counter 
    1;
    WHILE (
    file_exists($uploadFilename $uploadsDirectory.$_FILES[$fieldname"."$counter."."]['name'])){
        
    $counter++;
        } 

  9. #99
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by waynesimmonds
    Yeh thats correct, i need the original filename of the image that is being uploaded with a counter variable being added onto the end of the filename not before as the post above your last states
    That is what the code in post #94 does.

  10. #100
    Join Date
    Mar 2007
    Posts
    7
    Hate to be a bother... but I think my question might have been lost with the new one being asked...

    I have been going over this for 2 days now and I still can not figure out why it is giving me the error I described, any tips?

  11. #101
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by minnesotastereo
    I am not trying to get this location stored in my DB so I can later call on it and display the image on my site. Here is my code
    Are you sure that is what you mean?

    Also if you are getting that error you need to echo $uploadFilename and find out if what it contains is what you expect.

  12. #102
    Join Date
    Mar 2007
    Posts
    7
    Quote Originally Posted by minnesotastereo
    Oops. Can't find an edit button....


    Should be "I am NOW trying..."
    Yes, I meant to say now.

    I will try echoing this. Thanks for the reply, will post results when done.
    Andy

  13. #103
    Join Date
    Mar 2007
    Posts
    7
    Alright here is what happens:
    Code:
    ArrayINSERT INTO model_images VALUES ('', '', 'Array', '', '')

    That is what the page multiple.upload.processor.php is displaying.

    It seems it is not recieving the values for:
    Code:
    $hardwareId = $_POST['hardwareId']; 
    $modelId = $_POST['modelId']; 
    $brandID = $_POST['brandID'];
    Which I am trying to pass from multiple.upload.form.php file.
    I did echo on these values on multiple.upload.form.php and it outputs the correct values. Did I pass these values along wrong? Here is the code I am using right now in the multiple.upload.processor.php:

    Code:
    // now let's move the file to its final and allocate it with the new filename
    foreach($active_keys as $key)
    {
    	@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
    		or error('receiving directory insuffiecient permission', $uploadForm);
    }
    	
    // my code additions
    $hardwareId = $_POST['hardwareId']; 
    $modelId = $_POST['modelId']; 
    $brandID = $_POST['brandID']; 
    
    echo $uploadFilename;
    $query = "INSERT INTO model_images VALUES ('', '$modelId', '$uploadFilename', '$hardwareId', '$brandID')";
    if (!($result = @ mysql_query ($query,$connection))) showerror;
    echo $query;
    //end my code additions
    
    // If you got this far, everything has worked and the file has been successfully saved.
    // We are now going to redirect the client to the success page.
    header('Location: ' . $uploadSuccess);
    Here is the code I added into mutiple.upload.form.php:
    Code:
    // set a max file size for the html upload form
    $max_file_size = 1200000; // size in bytes
    
    
    
     echo "Brand ID is "      . $brandID  . "\n";
     echo "Model ID is "      . $modelId  . "\n";
     echo "hardware ID is "      . $hardwareId  . "\n";
     
     $_POST['brandID'];
     $_POST['modelId'];
     $_POST['hardwareId'];
     $brandID = $_POST['brandID'];
     $modelId = $_POST['modelId'];
     $hardwareId = $_POST['hardwareId'];
    // now echo the html page
    Here is the code to the page that sends this page the brandID, modelId, and hardwareId values (this i know works because it echo's the correct values at the top of multiple.upload.form.php):
    Code:
    <?php
    
    
    #####################################################################
       include 'db.php'; //This calls on the database info
       include 'error.php'; // this calls on the error function
    #####################################################################   
       
       
    #####################################################################   
      // echo $_POST['brandID']; // To see if the brandID is being passed correctly, simply uncomment this code and view the site
    #####################################################################
      
    #####################################################################  
       $brandname = $_POST['brandname']; 
       $hardware = $_POST['hardware'];
    #####################################################################   
       
       
    #####################################################################   
    // this code retrieves the information from the database, and passes it along to be printed
       $query = "SELECT bn.brand_title,  
                         sai.model_number,
                         sai.description,
                         sai.manufactured_origin,
                         sai.wood_case,
                         sai.weight,
                         sai.model_id_number,
                         bn.brand_id_number,
                         h.hardware_name,
                         h.hardware_id
                 FROM hardware h, brand_name bn, stats_and_info sai, brand2model2type bmt
                 WHERE sai.description != \"\"
                 AND bmt.brand_id = bn.brand_id_number
                 AND bmt.hardware_id = h.hardware_id
                 AND bmt.model_id = sai.model_id_number
                 AND bn.brand_id_number = $brandname
                 AND bmt.hardware_id =$hardware
                 ORDER BY h.hardware_name ASC LIMIT 100";
    //end the code that retriieves the info from the database 
    #####################################################################
       // Open a connection to the DBMS
       if (!($connection = @ mysql_connect($hostname,
                                           $username,
                                           $password)))
          die("Could not connect to database");
    
       if (!mysql_select_db($databasename, $connection))
          showerror(  );
    
       // Run the query created above on the database through
       // the connection
       if (!($result = @ mysql_query ($query, $connection)))
          showerror(  );
    
       echo "\n<table border=\"0\">";
       // Process the INFO
       while ($row = @ mysql_fetch_array($result))
       {
          // Print a heading for the Information
          echo "\n<tr>\n\t<td bgcolor=\"maroon\">" .
               "<b><font color=\"white\">" .
               $row["brand_title"] . " " .
               $row["model_number"] . " " .
               $row["hardware_name"] . " ";    
          echo "</td>\n</tr>";
          // Below makes a link to where more info can be found on a specific brand+model+type
    echo "<tr align=\"right\"><td>" .
         "<a href=\"multiple.upload.form.php?brandID=" . $row["brand_id_number"] . "&amp;modelId=" .
         $row["model_id_number"] .  "&amp;hardwareId=" .
         $row["hardware_id"] .
         "\">Click Here to upload images for this model number</a>" .  
         "</td></tr>";
          // Blank row for better looks
          echo "\n<tr>\n\t<td></td>\n</tr>";
       }
    
       echo "\n</table>\n";
    
       if (!mysql_close($connection))
          showerror(  );
    ?>
    The one thing I know for sure is that the values hardwareId, modelId, and brandID pass successfully up to the multiple.upload.form.php file.

    Sorry for the lengthy post, trying to provide as much information as possible.

  14. #104
    Join Date
    May 2004
    Location
    The Big M
    Posts
    669
    Quote Originally Posted by waynesimmonds
    In response to the coment made by theuedimaster

    i modified the code in order to adopt your possible solution but stll had a parse error as follows.....

    Parse error: parse error, expecting `']'' in

    this is my modified code but i am not sure if i have entered it in the correct position as you mentioned???

    PHP Code:

    $counter 
    1;
    WHILE (
    file_exists($uploadFilename $uploadsDirectory.$_FILES[$fieldname"."$counter."."]['name'])){
        
    $counter++;
        } 
    I was thinking something more like this:

    PHP Code:
    $counter 1;
    $filenamearray explode("."$uploadsDirectory.$_FILES[$fieldname]['name']);
    WHILE (
    file_exists($uploadFilename $filenamearray[0].$counter.$filenamearray[1])){
        
    $counter++;

    This is of course assuming that you only have one period in the file name.

  15. #105
    Join Date
    Mar 2007
    Posts
    7
    Hello. A friend helped me out and I was able to get this working. THe problem was that $uploadFilename is an array. The code I found on this forum must have been for the single.upload code.

    Here is what I did to solve the issue:
    Code:
     $brandID =  $_GET['brandID'];
     $modelId =  $_GET['modelId'];
     $hardwareId =  $_GET['hardwareId'];
    if($uploadFilename[0] != ""){
    $query = "INSERT INTO model_images VALUES ('', '$modelId', '$uploadFilename[0]', '$hardwareId', '$brandID')";
    if (!($result = @ mysql_query ($query,$connection))) showerror;
    echo $query;
    }
    
    if($uploadFilename[1] != ""){
    $query = "INSERT INTO model_images VALUES ('', '$modelId', '$uploadFilename[1]', '$hardwareId', '$brandID')";
    if (!($result = @ mysql_query ($query,$connection))) showerror;
    echo $query;
    }
    
    if($uploadFilename[2] != ""){
    $query = "INSERT INTO model_images VALUES ('', '$modelId', '$uploadFilename[2]', '$hardwareId', '$brandID')";
    if (!($result = @ mysql_query ($query,$connection))) showerror;
    echo $query;
    }
    3 if statements that make sure the array is not empty. The other "id" variables were passed from the multiple.upload.form with this:
    Code:
    	<form id="Upload" action="multiple.upload.processor.php?brandID=<?php echo $_GET['brandID']  ?>&modelId=<?php echo $_GET['modelId'] ?>&hardwareId=<?php echo $_GET['hardwareId'] ?>" enctype="multipart/form-data" method="post">
    Those variables are id numbers for an items brandname, model name, and hardware type. The site I am doing is a list of vintage audio pieces.

    If anyone has any questions on what I did, please pm me and I will certainly try to help out. Thanks for the help you guys provided, and the great code! I am now going to try and change the $uploadFilename array's path to only reflect the image name so that I can call on the database to display the images.
    Andy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles