image uploading script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Raju B
    New Member
    • Aug 2010
    • 11

    image uploading script

    hi friends
    i had written a code for uploading image.but it showing error like failed to open stream on line 61.please go through my code and suggest me.
    Code:
    <?php
    //include 'include/dbconnect.php';
      
    //define a maxim size for the uploaded images in Kb
     define ("MAX_SIZE","1000");
    
    //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
     function getExtension($str) {
             $i = strrpos($str,".");
             if (!$i) { return ""; }
             $l = strlen($str) - $i;
             $ext = substr($str,$i+1,$l);
             return $ext;
     }
    
    //This variable is used as a flag. The value is initialized with 0 (meaning no error  found) 
    //and it will be changed to 1 if an errro occures. 
    //If the error occures the file will not be uploaded.
     $errors=0;
    //checks if the form has been submitted
     if(isset($_POST['submit']))
     {
         //reads the name of the file the user submitted for uploading
         $image=$_FILES['image']['name'];
    	 $name=$_POST['imagename'];
         //if it is not empty
         if ($image)
         {
         //get the original name of the file from the clients machine
             $filename = stripslashes($_FILES['image']['name']);
         //get the extension of the file in a lower case format
              $extension = getExtension($filename);
             $extension = strtolower($extension);
         //if it is not a known extension, we will suppose it is an error and will not  upload the file, 
        //otherwise we will do more tests
     if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
             {
            //print error message
                 $result="cant upload,as you have chosen a incorrect file";
                 $errors=1;
             }
             else
             {
    //get the size of the image in bytes
     //$_FILES['image']['tmp_name'] is the temporary filename of the file
     //in which the uploaded file was stored on the server
     $size=filesize($_FILES['image']['tmp_name']);
    
    //compare the size with the maxim size we defined and print error if bigger
    if ($size > MAX_SIZE*1024)
    {
        $result="you exceeded the file size to upload";
        $errors=1;
    }
    
    //we will give an unique name, for example the time in unix time format
    $image_name=$name.'.'.$extension;
    //the new name will be containing the full path where will be stored (images folder)
    $newname="images/".$image_name;
    //we verify if the image has been uploaded, and print error instead
    $copied = copy($_FILES['image']['tmp_name'], $newname);
    
    if (!$copied)
    {
        $result="cant upload try after some time";
        $errors=1;
    }
         }}
         else
         $result_error="please select an image to upload";
         }
    
    //If no errors registred, print the success message
     if(isset($_POST['submit']) && !$errors && $image)
     {
       
       $result="image uploaded successfully";
     }
    
     ?>
    <html>
    <head>
    <style>
    #button{position:absolute;margin-left:323px;margin-top:13px;}
    #result{position:absolute;margin-left:170px;margin-top:75px;width:400px;color:green;}
    #result_error{position:absolute;margin-left:170px;margin-top:75px;width:400px;color:red;}
    
    </style>
    </head>
    <body>
    <FORM METHOD=POST ENCTYPE="multipart/form-data" ACTION="#">
    Select the image :<INPUT TYPE=FILE NAME="image"><BR>
    <input type="text" name="imagename"><br>
    <INPUT TYPE=SUBMIT name="submit" id="button" VALUE="Upload">
    </FORM>
    <div id="result"><?php echo $result;?></div>
    <div id="result_error"><?php echo $result_error;?></div>
    
    </body>
    
    </html>
    
    
    
    </div>
    
    </body>
    </html>
  • Mayur2007
    New Member
    • Aug 2007
    • 67

    #2
    Hello Raju B,

    I had made some changes in your script. Please go through it.

    Please use proper HTML tags that I had not changed otherwise may be it will create problems.

    Regards,
    Mayur Bhayani
    Code:
    <?php
    //include 'include/dbconnect.php';
     
    //define a maxim size for the uploaded images in Kb
     define ("MAX_SIZE","1000");
     
    //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
     function getExtension($str) {
             $i = strrpos($str,".");
             if (!$i) { return ""; }
             $l = strlen($str) - $i;
             $ext = substr($str,$i+1,$l);
             return $ext;
     }
     
    //This variable is used as a flag. The value is initialized with 0 (meaning no error  found) 
    //and it will be changed to 1 if an errro occures. 
    //If the error occures the file will not be uploaded.
     $errors=0;
    //checks if the form has been submitted
     if(isset($_POST['submit']))
     {
         //reads the name of the file the user submitted for uploading
         $image=$_FILES['image']['name'];
         $name=$_POST['imagename'];
         //if it is not empty
         if ($image)
         {
         //get the original name of the file from the clients machine
             $filename = stripslashes($_FILES['image']['name']);
         //get the extension of the file in a lower case format
              $extension = getExtension($filename);
             $extension = strtolower($extension);
         //if it is not a known extension, we will suppose it is an error and will not  upload the file, 
        //otherwise we will do more tests
     if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
             {
            //print error message
                 $result="cant upload,as you have chosen a incorrect file";
                 $errors=1;
             }
             else
             {
    //get the size of the image in bytes
     //$_FILES['image']['tmp_name'] is the temporary filename of the file
     //in which the uploaded file was stored on the server
     $size=$_FILES['image']['size'];
     
    //compare the size with the maxim size we defined and print error if bigger
    if ($size > MAX_SIZE*1024)
    {
        $result="you exceeded the file size to upload";
        $errors=1;
    }
     
    //we will give an unique name, for example the time in unix time format
    $image_name=$name.'.'.$extension;
    //the new name will be containing the full path where will be stored (images folder)
    $newname="images/".$image_name;
    //we verify if the image has been uploaded, and print error instead
    $copied = copy($_FILES['image']['tmp_name'], $newname);
     
    if (!$copied)
    {
        $result="cant upload try after some time";
        $errors=1;
    }
         }}
         else
         $result_error="please select an image to upload";
         }
     
    //If no errors registred, print the success message
     if(isset($_POST['submit']) && !$errors && $image)
     {
     
       $result="image uploaded successfully";
     }
     
     ?>
    <html>
    <head>
    <style>
    #button{position:absolute;margin-left:323px;margin-top:13px;}
    #result{position:absolute;margin-left:170px;margin-top:75px;width:400px;color:green;}
    #result_error{position:absolute;margin-left:170px;margin-top:75px;width:400px;color:red;}
     
    </style>
    </head>
    <body>
    <FORM METHOD=POST ENCTYPE="multipart/form-data" ACTION="#">
    Select the image :<INPUT TYPE=FILE NAME="image"><BR>
    <input type="text" name="imagename"><br>
    <INPUT TYPE=SUBMIT name="submit" id="button" VALUE="Upload">
    </FORM>
    <div id="result"><?php if(!empty($result)) echo $result;?></div>
    <div id="result_error"><?php if(!empty($result_error)) echo $result_error;?></div>
     
    </body>
     
    </html>
     
     
     
    </div>
     
    </body>
    </html>

    Comment

    • Raju B
      New Member
      • Aug 2010
      • 11

      #3
      thank you mayur,
      but html tags are not a problem here, because still it is showing same error.

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        On line 61, where you are recieving the error, replace the copy() function with move_uploaded_f ile(). copy() doesn't have access to the temporary upload directory, but move_uploaded_f ile() specifically created for that purpose.

        Comment

        Working...