Page 1 of 1

image gallery query (move_uploaded_file)

Posted: Sun Mar 23, 2008 6:48 pm
by frosty16
heya guys

i was wondering if anyone could help me with the following error.

Im just learngin php so dont really know what it means:

Code: Select all

move_uploaded_file(C:/webroot/gallery/images/gallery/1790da66bea8f3c8e84114d2f1e4a7a1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\AppServ\www\151SqnWebsite\picgallery\library\functions.php on line 21
 
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\TEMP\php167.tmp' to 'C:/webroot/gallery/images/gallery/1790da66bea8f3c8e84114d2f1e4a7a1.jpg' in C:\AppServ\www\151SqnWebsite\picgallery\library\functions.php on line 21
Error uploading file
line 21 reads :

Code: Select all

$result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
Thank you in advance

frosty

Re: image gallery query (move_uploaded_file)

Posted: Mon Mar 24, 2008 5:45 am
by it2051229
I think you have syntax error on your code.. would kindly post your code and let me see what's wrong

Re: image gallery query (move_uploaded_file)

Posted: Mon Mar 24, 2008 5:54 am
by frosty16
yeap the fuction that it is in is as follows:

Code: Select all

function uploadImage($inputName, $uploadDir)
{
    $image     = $_FILES[$inputName];
    $imagePath = '';
    $thumbnailPath = '';
    
    // if a file is given
    if (trim($image['tmp_name']) != '') {
        $ext = substr(strrchr($image['name'], "."), 1); 
 
        // generate a random new file name to avoid name conflict
        // then save the image under the new file name
        $imagePath = md5(rand() * time()) . ".$ext";
        $result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
            
        if ($result) {
            // create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";
            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
            
            // create thumbnail failed, delete the image
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }   
        } else {
            // the image cannot be uploaded
            $imagePath = $thumbnailPath = '';
        }
        
    }
 
    
    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
 
/*
    Create a thumbnail of $srcFile and save it to $destFile.
    The thumbnail will be $width pixels.
*/

Re: image gallery query (move_uploaded_file)

Posted: Mon Mar 24, 2008 8:50 am
by it2051229
yeah.. a lot of syntax error actually... well i can't explain all of them.. i'll just give you the script and it's up to you to find and compare your mistakes..

here's the script, you can copy and paste it in a PHP file and run it in your server.. hope this helps

Code: Select all

 
<?php
    /**
    * Resize Image Method
    * $imagePath = "where is the image to be resized?"
    * $width = "what width would you like the image to be resized?"
    * $height =  "what height would you like the image to be resized?"
    * Note: To use this function your PHP GD LIBRARY should be enabled
    */
    function resizeImage($imagePath, $width, $height)
    {
        // get the extention of the image like for example, if the image path is "/images/hello.jpg".. the "jpg" will be stored in the $ext variable
        $ext = explode(".", $imagePath);
        $ext = $ext[count($ext)-1];
      
        // check the extention type of the image if it is valid.. only JPG, JPEG, PNG, and GIF is allowed
        if($ext == "jpg" || $ext == "jpeg")
        {
            // copy the image which will be resized in JPG or JPEG format
            $im = imagecreatefromjpeg($imagePath);
        }
        elseif($ext == "png")
        {
            // copy the image which will be resized in PNG format
            $im = imagecreatefrompng($imagePath);
        }
        elseif($ext == "gif")
        {
            // copy the image which will be resized in GIF format
            $im = imagecreatefromgif($this -> imagePath);
        }
 
        // get the width and height of the image that is to be resized
        $x = imagesx($im);
        $y = imagesy($im);
 
        // check if the width and height of the image is COMPATIBLE with the given or specific width and height to be resized
        if($x <= $width && $y <= $height)
        {
             // if not valid, then do not resize the image, instead, just return and do nothing
             return $im;
        }
 
        // compute for the new width and new height of the image to be resized
        if($x >= $y)
        {
            $newx = $width;
            $newy = $newx * $y / $x;
        }
        else
        {
            $newy = $height;
            $newx = $x / $y * $newy;
        }
        
        // create the new resized image
        $im2 = imagecreatetruecolor($newx, $newy);
        imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
     
        // over write the old image and replace it with the new resized image
        // check again the extension of the image that is to be resized
        if($ext == "jpg" || $ext == "jpeg")
        {
            // create the new resized image in JPEG and overwrite the old image where quality of image is 100
            imagejpeg($im2, $imagePath,100);
        }
        else if($ext == "png")
        {
             // create the new resized image in PNG and overwrite the old image where quality of image is 100
             imagepng($im2, $imagePath,100);
        }
        else if($ext == "gif")
        {
             // create the new resized image in GIF and overwrite the old image. GIFs does not have image quality settings
             imagegif($im2, $imagePath);
        }
        return;
    }
    
    // check if the resize button is triggered
    if(isset($_POST["resizeButton"]))
    {
        // if triggered, get the image that was browsed and store them in a variable
        $imageTmpName = $_FILES["theImage"]["tmp_name"]; // the real path of the image with the temporary name attached
        $imageName = $_FILES["theImage"]["name"]; // just the name of the image without the path
        
        // validate the browsed image if it is a JPEG or JPG or PNG or GIF
        // so what we do is to get the extension of the file and check it
        // how to get the extention?
        // 1] we explode the image name like let's say the name of the image is "1234.jpg", if we explode the image,
        //    the output would be an array like 
        //    "[1234][jpg]"
        //        0    1 <---------- index of the array
        // 2] Next is we need to get the "jpg" part and it is expected that all extention of files are always located
        //    at the very end of the filename so we use the "end" function of PHP to get the "jpg" part.
        // 3] After getting the "jpg" we try to convert it to UPPER CASE so it would be like "JPG".. WHY? because there are
        //    instances where the file name would be like "jpG" or "JpG" or "jPg" and it would be better if we CAPITALIZE
        //    everything so there would be less comparing to happen which is the next thing we do.
        $extension = strtoupper(end(explode(".", $imageName)));
        
        // after getting the extension, we check if the extension of the file is an IMAGE(JPEG, JPG, GIF, or PNG)
        if($extension == "JPG" || $extension == "JPEG" || $extension == "GIF" || $extension == "PNG")
        {
            // if one of the file name extention is equal to the four IMAGE extentions, then the file is considered as an image.
            // The next thing we do is to SEND the IMAGE to the server
            move_uploaded_file($imageTmpName, $imageName);
            
            // after sending the image to the server, next is to RESIZE the image by calling the resize image function
            // and this time I want the image to be on a 100 by 100 pixel size.
            resizeImage($imageName,"100","100");
            
            echo "<h1><font color='red'>THE IMAGE YOU HAVE UPLOADED HAS BEEN SUCCESSFULLY SENT TO THE SERVER AND RESIZED GO CHECK IT OUT!!!</font></h1>";
        }
        else
        {
            echo "<h1><font color='red'>WTF??? ARE YOU SURE YOU ARE UPLOADING AN IMAGE??</font></h1>";
        }
    }
?>
<html>
<head><title>UPLOAD AND RESIZE</title></head>
<body>
<h1>Instruction</h1>
<p>
    Ok here I created a small script for upload of an image(JPEG or JPG, PNG, and GIF) and the image will be stored
    on the server side and it will be resized.. <br /><br />
    
    <b>Program Architecture.. A little of explanation just to let you know how the program runs on the back end</b><br />
    1.) The client will browse the image that is to be resized.<br />
    2.) If the image is valid, it will be SENT and SAVED on the server.<br />
    3.) After it is saved, the PHP resize image function will COPY the original image.<br />
    4.) The copied image will be resized.<br />
    5.) After the copied image is resized, it will overwrite the original image.<br /><br />
    
    <b><font color='red'>Note</font></b><br />
    1.) Though it's not going to be displayed and it's up to you to find it out how to display it on the browser.<br />
    2.) The image will not go back to the client to be downloaded and thats up to you how to find out.<br />
    3.) Your PHP GD Library should be enabled to use the IMAGE FUNCTIONS of the PHP.<br />
</p>
<h1>UPLOAD AND RESIZE</h1>
<form method='post' enctype='multipart/form-data' action='<?php echo $_SERVER["PHP_SELF"] ?>'>
    <b>Browse the Image you want to resize</b><br />
    <input type='file' name='theImage'><br /><br />
    <input type='submit' name='resizeButton' value='Send the Image to Server and Resize'>
</form>
</body>
</html>