www.webdeveloper.com
Page 9 of 23 FirstFirst ... 789101119 ... LastLast
Results 121 to 135 of 340

Thread: How to: Upload images using PHP

  1. #121
    Join Date
    Jun 2006
    Posts
    159
    Quote Originally Posted by PJStew
    ok, I understand what you are saying, but i'm not sure exactly how to do that..? do i have to do multiple database submission? i.e.
    PHP Code:
    $q "INSERT INTO uploaded_images (`user_name`,`filename`,`thumbnail`,`comment`,`date`,`style`) VALUES ('$username','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$uploadFilename[1])."','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$thumbFilename[1])."','$comment[1]',NOW(),'$style[1]')";
    mysql_query($q);
    }  

    $q "INSERT INTO uploaded_images (`user_name`,`filename`,`thumbnail`,`comment`,`date`,`style`) VALUES ('$username','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$uploadFilename[2])."','".str_replace($_SERVER['DOCUMENT_ROOT'], ''$thumbFilename[2])."','$comment[2]',NOW(),'$style[2]')";
    mysql_query($q);

    and so on, or is there a quicker/less code way of doing it? or is that not what you mean at all? sorry for being so slow.

    Cheers for you help Bokeh.

    Pete
    I Still haven't managed to sort this out, can anybody help me out?

    Cheers

    Pete

  2. #122
    Join Date
    May 2003
    Posts
    1

    Automatic Thumbnailer

    First off, I have to say this is an excellent script, beautifully explained. Thank you.

    Did anyone figure out how to change this script so that it will automatically thumbnail a pic before it is uploaded to my photos folder (instead of setting the upload size allowed as is already done in this script)? What part of the code needs to be changed, and to what?

  3. #123
    Join Date
    Apr 2007
    Posts
    24
    Thanks for the great code Bokeh. But I have a question. I want to use this in a form where the users have to enter in some data, as well as upload the image. don't want to use a database, so is there some way to modify upload.processor.php to email the data from the fields in the form and the unique name of the file they uploaded. Here's an example of the form I've created for it.

    Code:
    <div class="ctw-form">
    <form action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
    <span class="ctw-label" style="padding-right: 14px;">Your name:</span>
    <input type="text" name="realname" size="20" />
    <br />
    <br />
    <span class="ctw-label">Pet's name(s):</span>
    <input type="text" name="email" size="20" />
    <br />
    <br />
    <span class="ctw-label" style="padding-right: 53px;">City:</span>
    <input type="text" name="city" size="20" />
    <br />
    <br />
    <span class="ctw-label" style="padding-right: 31px;">Country:</span>
    <select id='countrySelect' name='country' onchange='populateState()'>
    </select>
    <br />
    <br />
    <span class="ctw-label" style="padding-right: 46px;">State:</span>
    <select id='stateSelect' name='state'>
    </select>
    <script type="text/javascript">initCountry('US'); </script>
    <br />
    <br />
    Enter the path to your image or Browse:<br>
    <input type="file" name="upload" size="30">
    <br />
    <br />
    <input  id="submit" name="submit" type="submit" value="Submit" />
    <input name="reset" type="reset" value="Reset" />
    </form>		
    </div>
    I've tried already to add emailing code from another script I found, but I'm not having any luck with it.

  4. #124
    Join Date
    Apr 2007
    Posts
    24
    Ok, now I'm getting "Undefined index: file in blah blah blah" and "this is not an HTTP Upload." What's going on? I had this working at one point, tried adding in email functionality, didn't work, put all the original code back in and now it won't work. Any ideas?

  5. #125
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by CTWorion
    I want to use this in a form where the users have to enter in some data, as well as upload the image. [...] so is there some way to modify upload.processor.php to email the data from the fields in the form and the unique name of the file they uploaded.
    Something like this:
    PHP Code:
    <?php

        $fieldname 
    'your file fieldname';
        
    $form_data 'message content';
        
    $to        'some email';
        
    $from      'some email';
        
    $subject   'something';
        
        
    ################################################################################
        
        
    $message =         "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" .
                        
    "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" .
                        
    "<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n" .
                        
    "<head> \n" .
                        
    "  <meta http-equiv=\"content-type\" content= \n" .
                        
    "  \"text/html; charset=iso-8859-1\" /> \n" .
                        
    "<style type=\"text/css\"> \n" .
                        
    "body {    font-size: 9pt; font-family:  verdana, sans-serif;     color: #000; background:#fff; }  \n" .
                        
    ".bold { font-weight: bold; }  \n" .
                        
    "</style>  \n" .
                        
    "</head> \n" .
                        
    "<body>$form_data \n" .
                        
    "</body> \n" .
                        
    "</html> \n\n"
        
        
        
    $headers "From: $from";
        
        if(
    is_uploaded_file($_FILES[$fieldname]['tmp_name'])) 
        {
            
    $handle fopen($_FILES[$fieldname]['tmp_name'],'rb');
            
    $data fread($handle,filesize($_FILES[$fieldname]['tmp_name']));
            
    fclose($handle);
        
        
            
    // Generate a boundary string
            
    $semi_rand md5(time());
            
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
        
            
    // Add the headers for a file attachment
            
    $headers .= "\nMIME-Version: 1.0\n" .
                        
    "Content-Type: multipart/mixed;\n" .
                        
    " boundary=\"{$mime_boundary}\"";
        
            
    // Add a multipart boundary above the html message
            
    $message "This is a multi-part message in MIME format.\n\n" .
                       
    "--{$mime_boundary}\n" .
                       
    "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
                       
    "Content-Transfer-Encoding: 7bit\n\n" .
                       
    $message "\n\n"
                                
         
            
    // Base64 encode the file data
            
    $data chunk_split(base64_encode($data));
        
            
    //We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:
        
            // Add file attachment to the message
            
    $message .= "--{$mime_boundary}\n" .
                        
    "Content-Type: {$_FILES[$fieldname]['type']};\n" .
                        
    " name=\"{$fileatt_name}\"\n" .
                        
    "Content-Disposition: attachment;\n" .
                        
    " filename=\"{$_FILES[$fieldname]['name']}\"\n" .
                        
    "Content-Transfer-Encoding: base64\n\n" .
                        
    $data "\n\n" .
                        
    "--{$mime_boundary}--\n";
        }
        else
        {
            
    // Generate a boundary string
            
    $semi_rand md5(time());
            
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
            
            
    // Add the headers for a file attachment
            
    $headers .= "\nMIME-Version: 1.0\n" .
                        
    "Content-Type: multipart/mixed;\n" .
                        
    " boundary=\"{$mime_boundary}\"";
            
    // Add a multipart boundary above the html message
            
    $message "This is a multi-part message in MIME format.\n\n" .
                       
    "--{$mime_boundary}\n" .
                       
    "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
                       
    "Content-Transfer-Encoding: 7bit\n\n" .
                       
    $message "\n\n".
                       
    "--{$mime_boundary}--\n"
        }                        
        
        
    mail($to$subject$message$headers);

  6. #126
    Join Date
    Apr 2007
    Posts
    24
    Wow, thanks a lot. I assume I can just plug this code into what already exists in upload.processor.php. Question though ... I received the email letting me know that an image had been uploaded, but the body of the email just said "message content." I understand that the value assigned to $form_data is 'message content' so that's where it came from. But I need it to actually write the content of the form in there.

    Do I have to define a variable for every field like:
    Code:
    @$Name = addslashes($_POST['Name']);
    @$email = addslashes($_POST['email']);
    And then in the section that defines the message, replace form_data with the actual variables of the input fields? Does this sound about right?

  7. #127
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by CTWorion
    I understand that the value assigned to $form_data is 'message content' so that's where it came from. But I need it to actually write the content of the form in there.
    The cake is baked; all you need to do is add the icing.

  8. #128
    Join Date
    Apr 2007
    Posts
    24
    thanks a lot for the help. I finally got it working last night, and got the email looking the way I wanted. Thanks again for putting such wonderful code out there for people like me to learn from.

  9. #129
    Join Date
    Aug 2007
    Posts
    15
    Okay, so I used the Multiple Upload script, but it's not doing the following(im using the files provided in the .zip with no edits)

    Basically, I can't get the script to:
    Record Id/Date/Image Name in DB
    Provide working link on "success page" (Example: Hey! You've uploaded your image. Here's the link: http://domain.com/test/yourimage.jpg
    Editable extensions (allow/remove image extensions that can be uploaded)

    Any idea?

  10. #130
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Sketchy
    Any idea?
    That's because the script just contains the basics to do uploads. If you want more all you need to do is customize the script with some extra code.

  11. #131
    Join Date
    Aug 2007
    Posts
    2

    Strange: Works Well On Almost All, But Some Pictures Don't Display

    I have been using this code already and it works pretty well for me. In fact, the code has been used already for months. Until i get a feedback from my friend saying he uploaded an image on my site but the image does not display in the page. Other images display accordingly, but I found out that there are certain pictures not loading when they are already used. The image appears to be a broken link instead.

    The filename in the database as well as in the directory is the same. The assigned number as well as the extension are the same, even the letter case is the same!!! How come most pictures display when called but there are certain pictures that wont?

    I can't think of any possible reason for that. Please help.

    I have attached one particular image which is having a problem. I encountered about 5 in every 100 images that has this kind of problem.
    Attached Images Attached Images
    Last edited by taurus5_6; 08-31-2007 at 06:37 AM.

  12. #132
    Join Date
    Aug 2007
    Posts
    2

    Try Viewing My Image Attachment

    Great... I tried viewing the image i have just attached through the link, and it displays a broken link. I guess I need not explain further what I really meant.

    Quote Originally Posted by taurus5_6
    I have been using this code already and it works pretty well for me. In fact, the code has been used already for months. Until i get a feedback from my friend saying he uploaded an image on my site but the image does not display in the page. Other images display accordingly, but I found out that there are certain pictures not loading when they are already used. The image appears to be a broken link instead.

    The filename in the database as well as in the directory is the same. The assigned number as well as the extension are the same, even the letter case is the same!!! How come most pictures display when called but there are certain pictures that wont?

    I can't think of any possible reason for that. Please help.

    I have attached one particular image which is having a problem. I encountered about 5 in every 100 images that has this kind of problem.

  13. #133
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

  14. #134
    Join Date
    Jan 2006
    Posts
    50
    When I try to upload an image, I get the php.ini maxfile size error. However, I have edited my php.ini file to have the file_maxsize to be 10M and am trying to upload a 2M image. Smaller images are working, but the 2M file is not. What is wrong?

    Regards,
    Sam

  15. #135
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

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