www.webdeveloper.com
Page 10 of 23 FirstFirst ... 8910111220 ... LastLast
Results 136 to 150 of 340

Thread: How to: Upload images using PHP

  1. #136
    Join Date
    Oct 2007
    Posts
    1

    Thumbs up

    Quote Originally Posted by welsh
    yea works like a charm, easily adaptable to make a log with mysql.
    could you explain where in this script i would input the MySQL query to add the filename(s) to a table??

  2. #137
    Join Date
    Sep 2005
    Posts
    7

    Placing a variable of existing file in db into muliple upload form

    I've implemented the multiple upload script into a shopping cart editor to use for uploading a main image and thumbnail image. Thanks to the OP for this, its been a big help on my project. It works fine for inserting a new product, but I'm having trouble using the script for updating the database. Currently when the user goes to the page update an existing product they have to reselect an image from their local drive, rather than the files being automatically selected from the database. So my question is where do I place the variable for the existing files in database so they display automatically on the update page? (rather than it saying "no file selcted" it has the relevant image already stored in the form from the db).

  3. #138
    Join Date
    Sep 2005
    Posts
    7
    Quote Originally Posted by boognish
    I've implemented the multiple upload script into a shopping cart editor to use for uploading a main image and thumbnail image. Thanks to the OP for this, its been a big help on my project. It works fine for inserting a new product, but I'm having trouble using the script for updating the database. Currently when the user goes to the page update an existing product they have to reselect an image from their local drive, rather than the files being automatically selected from the database. So my question is where do I place the variable for the existing files in database so they display automatically on the update page? (rather than it saying "no file selcted" it has the relevant image already stored in the form from the db).
    Actually second thoughts... What I'd like to do is display the current image in the database on the update page, then when submitting the updated details, if no files are selected, instead of giving the error it just bypasses the upload and just updates the rest of the database. How do I get the script to bypass the upload if no file is selected instead of giving the error?

  4. #139
    Join Date
    Sep 2005
    Posts
    7
    I figured it out if anyone wants it for reference...

    update.php
    Code:
    $prodId=$_POST['prodId'];
    $prodName=$_POST['ud_name'];
    $price=$_POST['ud_price'];
    $description=$_POST['ud_description'];
    $subcat=$_POST['ud_subcategory_new']; 
    $subcat3=$_POST['ud_subcategory_new3'];
    $subcat4=$_POST['ud_subcategory_new4'];
    
    $updated2 = '/updated2.php?prodId='. $prodId .'&ud_name='. $prodName.'&ud_price='.$price.'&ud_description='.$description.'&ud_subcategory='.$subcat.'&ud_subcategory2='.$subcat2.'&ud_subcategory3='.$subcat3.'&ud_subcategory4='.$subcat4 ;
    
    ....
    
    // if no file selected go to alternative update file
    ($_FILES[$fieldname]['error'] == 0)
    	or header('Location: ' . $updated2);
    
    ..... Rest of file adding images and other content to db
    Then alternate update file that doesn't update images....

    Code:
    $prodId=$_GET['prodId'];
    $prodName=$_GET['ud_name'];
    $price=$_GET['ud_price'];
    $description=$_GET['ud_description'];
    $subcat=$_GET['ud_subcategory_new']; 
    $subcat3=$_GET['ud_subcategory_new3'];
    $subcat4=$_GET['ud_subcategory_new4'];
    
       $query = "UPDATE products SET prodName='$prodName', price='$price', description='$description', subcat='$subcat', subcat2='$subcat2', subcat3='$subcat3', subcat4='$subcat4' WHERE prodId='$prodId'";
    
    mysql_query($query) or die (mysql_error());
    mysql_close($dbl);
    
    $uploadSuccess = '/upload.success.php';	
    header('Location: ' . $uploadSuccess);

  5. #140
    Join Date
    Aug 2007
    Location
    London
    Posts
    410

    Unhappy

    having searched around for a solution, I couldn't find one... so here's the problem:

    I have my own website, which is essentially my portfolio: www.blueeyelabs.com
    I need an easy way of adding images, properly.
    I have a sort of way already that involves a php page to add the MySQL records and an automator script to upload etc the images.
    What I want is a php page that:
    -Uploads the images
    -Resizes and saves them in different locations according to requirement
    -Adds the records to the database
    -Adds a news item about the image

    Well, this is quite hard evidently, since I can't manage to do it properly.
    The script for adding records definitely works, I've tried.
    But the problem (I think) is to do with permissions, since I can't think of any reason why my script doesn't work (I've never even got a simpler version to work); it seems to break down on trying to save an image to a directory.
    I have tried chmod() commands, but they don't work, for some obscure reason, so here is the script (well, the image part, the rest of it I haven't bothered posting since it works).
    The errors I get are either:

    Code:
    FTP connection successful 
    FTP login successful 
    Image Paths: 
    http://www.blueeyelabs.com/portfolio/full/80.jpg
    http://www.blueeyelabs.com/portfolio/thumb/80.jpg
    http://www.blueeyelabs.com/portfolio/download/80.jpg
    http://www.blueeyelabs.com/portfolio/temp/80.jpg
    Image Progress: 
    ftp of dl file failed
    (if the file is marked as downloadable)
    or

    Code:
    FTP connection successful 
    FTP login successful 
    Image Paths: 
    http://www.blueeyelabs.com/portfolio/full/80.jpg
    http://www.blueeyelabs.com/portfolio/thumb/80.jpg
    http://www.blueeyelabs.com/portfolio/download/80.jpg
    http://www.blueeyelabs.com/portfolio/temp/80.jpg
    Image Progress: 
    DL image not required 
    unable to copy viewer image
    DL means "download": ie downloadable image
    viewer means the image that will be viewed as a big but not full size image
    thumbnail means just that; a thumbnail.

    Here is the script:
    PHP Code:
    //image functions and upload scripts
        //connect to ftp:
            
    $ftpCon ftp_connect('server') or die('connection failed (ftp)');
                echo 
    'FTP connection successful <br/>';
            
    $ftpResult ftp_login($ftpCon"usr""pwd") or die('login failed (ftp)');
                echo 
    'FTP login successful <br/>';
            
        
    //variables
                
    echo 'Image Paths: <br/>';
            
    $realFile $id ".jpg";
            
    $tmpFile $_FILES['file']['tmp_name'];
        
            
    $path "http://www.blueeyelabs.com/portfolio";
            
    $imageFile $path .  "/full/" $realFile;
                echo 
    $imageFile "<br/>";
            
    $thumbFile $path "/thumb/" $realFile;
                echo 
    $thumbFile "<br/>";
            
    $dlFile $path "/download/" $realFile;
                echo 
    $dlFile "<br/>";
            
    $tempFile $path "/temp/" $realFile;
                echo 
    $tempFile "<br/>";
        
        
    //we want a thumbnail and a full image, and maybe a downloadable image.
        //so let's resize.
            
    echo 'Image Progress: <br/>';
        
    //first copy the *download* file, if required
            
    if($dl == 1) {
                
                
    $upload ftp_put($ftpCon$dlFile$tmpFile0755) or die('ftp of dl file failed');
                    echo 
    'DL image successfully created <br/>';
            } else {
                echo 
    'DL image not required <br/>';
            }
        
    //then let's resize and copy the *Viewer* image:
            
    $src imagecreatefromjpeg($tmpFile);
            list(
    $width,$height) = getimagesize($tmpFile);
            
            
    //define target width/height
                
    $tWidth 800;
                
    $tHeight 600;
            
    //check if width or height are bigger, and resize accordingly.
                
    if($width $height) {
                    
    $newWidth $tWidth;
                    
    $newHeight = ($height $width) * $height;
                } elseif(
    $height $width) {
                    
    $newHeight $tHeight;
                    
    $newWidth = ($width $height) * $tWidth;
                } else {
                    die(
    'unable to resize viewer image');
                }
            
    //create new image
                
    $tmp2 imagecreatetruecolor($newWidth$newHeight);
            
    //ready, set, resize!
                
    imagecopyresampled($tmp2$src0000$newWidth$newHeight$width$height);
            
    //copy to directory
                
    imagejpeg($tmp2$imageFile100) or die('unable to copy viewer image');
            
    //destroy all temporary images
                
    imagedestroy($src);
                
    imagedestroy($tmp2);
            
                echo 
    'Viewer image successfully created <br/>';
        
    //onwards! To the thumbnail!
            
    $src imagecreatefromjpeg($tmpFile);
            list(
    $width,$height) = getimagesize($tmpFile);
            
            
    //define pre-crop target width/height
                
    $tWidth 800;
                
    $tHeight 600;
            
    //check if width or height are bigger, and resize accordingly.
                
    if($width $height) {
                    
    $newWidth $tWidth;
                    
    $newHeight = ($height $width) * $height;
                } elseif(
    $height $width) {
                    
    $newHeight $tHeight;
                    
    $newWidth = ($width $height) * $tWidth;
                } else {
                    die(
    'unable to resize pre-crop image');
                }
            
    //create new image
                
    $tmp2 imagecreatetruecolor($tWidth$tHeight);
            
    //copy new pre-crop image:
                
    imagecopyresampled($tmp2$src0000$tWidth$tHeight$width$height);
                
    $width $tWidth;
                
    $height $tHeight;
            
    //now we need crop target width and height:
                
    $tWidth 100;
                
    $tHeight 100;
            
    //in order to crop we need to find out where to start:
                //so, find halfway through the width/height and then subtract the target width/height:
                
    $cropStartX = ($width 2) - $tWidth;
                
    $cropStartY = ($height 2) - $tHeight;
            
    //ready, set, crop!
                
    imagecopyresampled($tmp2$src00$cropStartX$cropStartY$tWidth$tHeight$width$height);
            
    //copy to directory
                
    imagejpeg($tmp2$thumbFile100) or die('unable to copy thumbnail');
            
    //destroy temp images
                
    imagedestroy($src);
                
    imagedestroy($tmp2);
                
                echo 
    'Thumbnail image successfully created. <br/>';
                    
        
    //disconnect from ftp
            
    ftp_close($ftpCon);
        
            echo 
    'Image processing successful <br/>';
        
    //end image functions and upload scripts 
    Any help would be very much appreciated!

  6. #141
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by blue-eye-labs
    http://www.blueeyelabs.com/portfolio/download/80.jpg
    Thats a URL, not a remote file path. You can't "put" a file by URL; the receiving machine needs to know where to put the file within its filing system, not where to put it in cyberspace.

  7. #142
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    oh, I see, do I use $_SERVER[] for that then?
    The problem is that I can't use DOCUMENT_ROOT because my upload script is in a completely different directory (~/admin) to my image directory (~/portfolio).

  8. #143
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

  9. #144
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    unfortunately I log in to my domain name with ftp:
    no matter, I solved it with $_SERVER['DOCUMENT_ROOT'];

    I have a different problem with images, which I suppose I'll post elsewhere.

  10. #145
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by blue-eye-labs
    I have a different problem with images, which I suppose I'll post elsewhere.
    What is it?

  11. #146
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    This file upload script works very well. Thanks for providing the basics of it. It works flawlessly. I did make a few changes. I pass the file name on to the success.upload.php file, and then echo the URL into a text box (i.e. <input type='text' value='$filename' > ) . The user can then select the URL easily, and use it to link too, or whatever.

    My website has 'hot link protection' so you can't link images remotely. Can only link within the website. So if someone uploads a file it can only be viewed in the message forum. This is useful for those that don't have a good place to upload pictures too, but want an image in a message.

  12. #147
    Join Date
    May 2003
    Posts
    144
    First of all, thanks for the script.
    http://positive0.negative0.net/multi...pload.form.php

    I simply unzipped the file and uploaded them as it is. As you can see at the URL, the folder is located at the root. Try the link, the script isn't working.

    1. Do I have to add and edit some scripts in there?

    2. If not, What the problem I'm facing?

    Michael

    P.S. except, I changed this
    PHP Code:
    // set a max file size for the html upload form
    $max_file_size 3000000000000// size in bytes
    // since the images might be bigger than the normal ones 
    Last edited by Michaelttkk; 11-13-2007 at 10:22 PM.
    Learning never ends!

  13. #148
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    it seems to be getting a "not an image" problem which would suggest that you're not actually playing with the image itself, since I got a rather odd name for the temporary file... If this isn't a public form it might be worth trying to reduce some of that validation just to see if it works...

  14. #149
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Michaelttkk
    2. If not, What the problem I'm facing?
    getimagesize() cant read the image. Remove the '@' symbol in front of getimagesize and see if the script raises an error. If not, raise the error_reporting level until one is shown. It may be a permission problem.
    Quote Originally Posted by blue-eye-labs
    I got a rather odd name for the temporary file
    Odd names are par for the course with temporary files.

  15. #150
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    @bokeh:
    I have wondered why you put the @ sign in front of a great many of your commands...
    What I meant by odd is that there wasn't a file extension.... but maybe that's just the way it is with temp files

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 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