Dynadot โ€” .com Registration $8.99

PHP uploading problems

Spaceship Spaceship
Watch
Impact
111
Hello...

I have an upload script that I found online (I've only worked with uploads once in my life, and it didn't involve image resizing):

PHP:
/*== only resize if the image is larger than 660 x 1000 ==*/
    $imgsize = GetImageSize($imgfile);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 660) || ($imgsize[1] > 1000))
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type)
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/

        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile >$tmpimg");


        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 660 1000 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

        /*== remove temp image ==*/
        unlink($tmpimg);

    }

    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename  ==*/
    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir . "/$final_filename";

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);


    //print("<img src=\"$final_filename\">");


    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile,"$newfile"))
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }

The error comes on the line with the unlink($tmpimg) function at the end of the resizing where it deletes the temporary file. It says there's no such file or directory, yet there's no other errors given out.

Any help appreciated :) Thanks.

.chulium.

(I didn't include *ALL* of the code, like the form, etc, but everything else is working fine. I've spent hours trying to debug this script... trust me on the previous code: it's good.)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
You probably don't need to have that line.. it's a temp file and will probably be deleted anyways.
 
0
•••
You don't need to delete the tmp file. The server will do it for you and PHP cannot access the /tmp folder. Does it work otherwise?

- Steve
 
0
•••
No, the upload doesn't work at all... I CHMOD'ed everything to 777 (well, everything being the upload directory I made)...
 
0
•••
did u put <php? in front of the script and ?> at the end?
 
0
•••
I did of course :)

Any other ideas? :-/
 
0
•••
1, where are you defining the $_FILE ? all I see is $imgfile
2, does your server support Imagemajik/system() commands?
3, is $uploaddir set anywhere and to the same folder you have chmodd?
4, Have you tried dumping what is uploaded i.e.
PHP:
print_r($_FILES);
5, You may want to show your form to make sure you are allowing the enctype upload.
 
0
•••
This might be easier... here's my whole code:

PHP:
<?php

include("functions.php");

function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }

$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

if (!$_GET['go'])
{
?>
<html>
<head>
<title>Upload New Picture</title>
</head>
<body>
<h1>Upload New Photograph</h1>

(Max file size: 2.0 MB)<br><br>
<form method="post" action="newimg.php?go=true" enctype="multipart/form-data" id="mf" name="mf">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Image: <input type="file" name="imgfile">
<br><br>
Caption: <input type="text" size="40" maxlength="100" name="title">
<br><br>
Description: <textarea cols="40" rows="5" name="des"></textarea>
<br><br>
Category: <select size="1" name="catid">
<option value="" SELECTED></option>
<?php
$q = mysql_query("SELECT * FROM categories ORDER BY title ASC");
$i = 0;
while ($r = mysql_fetch_array($q))
{
	$title = stripslashes($r['title']);
	$id = $r['id'];
?>
<option value="<?php echo($id); ?>"><?php echo($title); ?></option>
<?php
}
?>
</select>
<br><br>
Make Category Thumbnail? <select size="1" name="catrep">
<option value="No" SELECTED>No</option>
<option value="Yes">Yes</option>
</select>
<br><br>
Comments: <select size="1" name="comments">
<option value="On" SELECTED>On</option>
<option value="Off">Off</option>
</select>
<br><br>
<input type="submit" value="Upload Image" id="sbt" onClick="this.disabled=true;return true;"> ย  <a href="main.php">Menu</a>
</form>

<br><br><br>

(c) 2007 ****.

</body>
</html>
<?php
}
else
{

	$imgfile = $_POST['imgfile'];
	//Duplicate it. I hope this works!
	$imgfile2 = $imgfile;
	//End Dupe.
	$title = $_POST['title'];
	$des = $_POST['des'];
	$cat = $_POST['cat'];
	$catrep = $_POST['catrep'];
	$comments = $_POST['comments'];

	$now = date("F d, Y");



    /* SUBMITTED INFORMATION - use what you need
     * temporary filename (pointer): $imgfile
     * original filename           : $imgfile_name
     * size of uploaded file       : $imgfile_size
     * mime-type of uploaded file  : $imgfile_type
     */

     $imgfile_name = $_FILES['imgfile']['name'];
     $imgfile2_name = $_FILES['imgfile']['name'];
     $imgfile_size = $_FILES['imgfile']['size'];

     /*== upload directory where the file will be stored
          relative to where script is run ==*/

    $uploaddir = "../imgstore/";

    /*== get file extension (fn at top of script) ==*/
    /*== checks to see if image file, if not do not allow upload ==*/
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

        /*== delete uploaded file ==*/
        unlink($imgfile);
        unlink($imgfile2);
        exit();
    }

	$thisimgrnd = rand(100, 999);

    $imgfile_name = $thisimgrnd."_".$imgfile_name;
	$imgfile2_name = $thisimgrnd."_thumb_".$imgfile2_name;

    //-- RE-SIZING UPLOADED IMAGE - 100x70

    /*== only resize if the image is larger than 100 x 70 ==*/
    $imgsize2 = GetImageSize($imgfile2);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize2[0] > 100) || ($imgsize2[1] > 70))
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the
            script at once they won't ruin each other's temp file ==*/
        $tmpimg2 = tempnam("/tmp/", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type)
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/

        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile2 >$tmpimg2");


        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 100 70 $tmpimg2 | cjpeg -smoo 10 -qual 50 >$imgfile2");

        /*== remove temp image ==*/
        unlink($tmpimg2);

    }

    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename  ==*/
    $final_filename2 = str_replace(" ", "_", $imgfile2_name);
    $newfile2 = $uploaddir . "/$final_filename2";

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile2);

	if (is_uploaded_file($imgfile2))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile2,"$newfile2"))
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }

    //print("<img src=\"$final_filename2\">");





    //-- RE-SIZING UPLOADED IMAGE - 660x1000

    /*== only resize if the image is larger than 660 x 1000 ==*/
    $imgsize = GetImageSize($imgfile);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 660) || ($imgsize[1] > 1000))
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type)
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/

        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile >$tmpimg");


        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 660 1000 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

        /*== remove temp image ==*/
        unlink($tmpimg);

    }

    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename  ==*/
    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir . "/$final_filename";

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);


    //print("<img src=\"$final_filename\">");


    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile,"$newfile"))
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }

    /*== DO WHATEVER ELSE YOU WANT
         SUCH AS INSERT DATA INTO A DATABASE  ==*/





	//Add to db.

	
?>
<html>
<head>
<title>Upload New Picture</title>
</head>
<body>
<h1>Photo successfully uploaded. (hopefully)</h1>

<img src="../imgstore/<?php echo($final_filename2); ?>" />
<br><br>
<a href="main.php">Menu</a> | <a href="newimg.php">UPLOAD ANOTHER?</a>


<br><br><br>

(c) 2007 ***.

</body>
</html>
<?php
}
?>

It's not pretty... it's not secure... it's not FINISHED. So before posting anything off-topic of what I want fixed, consider that :) Thanks.

I actually have little idea of what I'm doing... :hehe: So um, be nice :)

Thanks! Help appreciated. Rep +
.chulium.
 
0
•••
Code:
$imgfile = $_POST['imgfile'];
should be
Code:
$imgfile = $_FILES['imgfile']['name'];
or
Code:
$imgfile = $_FILES['imgfile']['tmp_name'];
 
1
•••
Nope... that just made 2 more errors (both methods):

Code:
Warning: getimagesize(test.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/***/public_html/imgadmin/newimg.php on line 138

Warning: unlink(test.jpg) [function.unlink]: No such file or directory in /home/***/public_html/imgadmin/newimg.php on line 173

Warning: getimagesize(test.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/***/public_html/imgadmin/newimg.php on line 197

Warning: unlink(test.jpg) [function.unlink]: No such file or directory in /home/***/public_html/imgadmin/newimg.php on line 232

:(


I do want to point out though that I'm duplicating the image variable ($imgfile2 and $imgfile) just on submission so I can turn one into a thumbnail, and just trim the other one down to size.
 
0
•••
...I still need help with this. Please? :)

Suppose I offered a $10 USD PayPal reward when this works right?
 
0
•••
Is your file even uploading? I prefer to use move_uploaded_file() and then make any changes to the file. Safe_mode restrictions normally prevent you from working on a file in the original tmp directory.
 
1
•••
It's not uploading :(

I put in a quick error checker and changed the is_uploaded_file() to move_uploaded_file() and added the proper arguments for file and destination.

I get an error now that it won't upload.... yet I'm not 100% sure why. Can anybody maybe write a quick, clean upload script as the ones I see on Google are either too simple, too complicated, or all contradict each other in methodology.
 
0
•••
Use hotscripts and find something free. Lots of things out there if you look hard enough. If you want a fairly full featured upload script try celerondude.com's script.
 
0
•••
The php manual has a very detailed section on file uploads, including some sample code which has always worked for me and serves as the foundation for numerous image upload and manipulation scripts I've written: http://us3.php.net/manual/en/features.file-upload.php

IMPORTANT: read the user comments at the bottom of the page. Lots of suggestions for things to look at if your upload still fails (various php.ini settings, permissions, etc.)
 
0
•••
Thanks both.

labrocca: I am using a simple, self-programmed gallery script for my photography site. Thanks for the suggestion, though.

cef: I'll check it out; danke.

If I still need help I'll update this thread.
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back