71 users online (1 members and 70 guests)  


  Results 1 to 2 of 2

Related

  1. Replies: 0
  2. Running a batch script file from Perl CGI    Forum: CGI Perl Forum
    Replies: 0
  3. 5 Adsense Websites with Loads of Content    Forum: Websites For Sale
    Replies: 0
  4. Browser loads the same page, help    Forum: PHP Forum
    Replies: 2
  5. Replies: 2
  1. #1
    lozza's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2008
    Posts
    11

    PHP script running when page loads

    Hi all - hoping someone will be able to help me with this one...

    I Have a script that resizes images and inserts them into a folder, and it works. Only problem is that it gives an error when the page initially loads. However, I can still upload an image, and when the page posts back to itself I get NO errors. How can I keep my script from running when the page first loads? I want it to run ONLY when the form is submitted. I am thinking something like:

    Code:
     if(isset($_POST['submit']))
    But I'm not sure the exact form. My working code that gets errors is below.

    Code:
    <form action="insertimages.php" method="post" enctype="multipart/form-data" >
    <input type="file" name="image_file"/>
    <input type="submit"/>
     
    <?php
     
    // This is the temporary file created by PHP
    $uploadedfile = $_FILES['image_file']['tmp_name'];
    
    // Create an Image so we can do the resize
    $src = imagecreatefromjpeg($uploadedfile);
    
    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);
    
    
    $newwidth=200;
    $newheight=($height/$width)*200;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    
    // image into the $tmp image
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    
    $filename = "../root folder/images/". $_FILES['image_file']['name'];
    imagejpeg($tmp,$filename,100);
    
    imagedestroy($src);
    imagedestroy($tmp); 
    
    ?>
    Any help appreciated!!


    UPDATE!
    With the added lines of:

    Code:
    if ($uploadedfile != "") {
    echo
    inserted just below the calling of the "$uploadedfile" variable I just get a message saying "Resource id #7 "...closer, but not there yet. Any ideas?
    Last edited by vinyl-junkie; 11-01-2008 at 09:41 AM. Reason: Added code tags

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: PHP script running when page loads

    You are on the right track. Try the following code:

    Code:
    <form action="insertimages.php" method="post" enctype="multipart/form-data" >
    <input type="file" name="image_file" />
    <input type="submit" name="submit" />
     
    <?php
    if (isset($_POST['submit'])) {
    	// This is the temporary file created by PHP
    	$uploadedfile = $_FILES['image_file']['tmp_name'];
    
    	if ($uploadedfile != "") {
    		// Create an Image so we can do the resize
    		$src = imagecreatefromjpeg($uploadedfile);
    
    		// Capture the original size of the uploaded image
    		list($width,$height)=getimagesize($uploadedfile);
    
    		$newwidth=200;
    		$newheight=($height/$width)*200;
    		$tmp=imagecreatetruecolor($newwidth,$newheight);
    
    		// image into the $tmp image
    		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    
    		$filename = "../root folder/images/". $_FILES['image_file']['name'];
    		imagejpeg($tmp,$filename,100);
    
    		imagedestroy($src);
    		imagedestroy($tmp); 
    	} else {
    		echo "No image was uploaded";
    	}
    }
    ?>



Tags for this Thread