NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP uploading problems

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-03-2007, 02:01 PM THREAD STARTER               #1 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)

PHP uploading problems


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 Code:
/*== 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);
????: NamePros.com http://www.namepros.com/programming/300795-php-uploading-problems.html

    }

    
/*== 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))
????: NamePros.com http://www.namepros.com/showthread.php?t=300795
    {

       
/*== 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.)
mholt is offline  
Old 03-03-2007, 02:08 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
You probably don't need to have that line.. it's a temp file and will probably be deleted anyways.
Dan is offline  
Old 03-03-2007, 02:10 PM   #3 (permalink)
Eating Pie
 
iNod's Avatar
Join Date: Nov 2004
Location: Canada
Posts: 2,272
iNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud of
 


Special Olympics AIDS/HIV Cystic Fibrosis Save The Children Baby Health Cystic Fibrosis
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
__________________
I feel old.
iNod is offline  
Old 03-03-2007, 02:15 PM THREAD STARTER               #4 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
No, the upload doesn't work at all... I CHMOD'ed everything to 777 (well, everything being the upload directory I made)...
mholt is offline  
Old 03-04-2007, 12:39 PM   #5 (permalink)
NamePros Regular
 
smilgy's Avatar
Join Date: Jan 2007
Posts: 255
smilgy is on a distinguished road
 



did u put <php? in front of the script and ?> at the end?
smilgy is offline  
Old 03-04-2007, 12:52 PM THREAD STARTER               #6 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
I did of course

Any other ideas? :-/
mholt is offline  
Old 03-04-2007, 02:42 PM   #7 (permalink)
NamePros Member
Join Date: Jan 2005
Posts: 112
lfhost is on a distinguished road
 



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 Code:
print_r($_FILES); 
????: NamePros.com http://www.namepros.com/showthread.php?t=300795
5, You may want to show your form to make sure you are allowing the enctype upload.
__________________
Thanks
Paul
1|2|3|4|5|6|7|8|9|10|11|12|13
lfhost is offline  
Old 03-04-2007, 04:44 PM THREAD STARTER               #8 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
This might be easier... here's my whole code:

PHP Code:
<?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;"> &nbsp; <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(100999);

    
$imgfile_name $thisimgrnd."_".$imgfile_name;
    
$imgfile2_name $thisimgrnd."_thumb_".$imgfile2_name;
????: NamePros.com http://www.namepros.com/showthread.php?t=300795

    
//-- 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==*/
????: NamePros.com http://www.namepros.com/showthread.php?t=300795
    
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... So um, be nice

Thanks! Help appreciated. Rep +
.chulium.
mholt is offline  
Old 03-04-2007, 04:58 PM   #9 (permalink)
NamePros Member
Join Date: Jan 2005
Posts: 112
lfhost is on a distinguished road
 



Code:
$imgfile = $_POST['imgfile'];
should be
Code:
$imgfile = $_FILES['imgfile']['name'];
or
Code:
$imgfile = $_FILES['imgfile']['tmp_name'];
__________________
Thanks
Paul
1|2|3|4|5|6|7|8|9|10|11|12|13
lfhost is offline  
Old 03-04-2007, 05:44 PM THREAD STARTER               #10 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
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.
mholt is offline  
Old 03-05-2007, 05:43 PM THREAD STARTER               #11 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
...I still need help with this. Please?

Suppose I offered a $10 USD PayPal reward when this works right?
mholt is offline  
Old 03-05-2007, 07:41 PM   #12 (permalink)
NamePros Expert
 
labrocca's Avatar
Join Date: Aug 2004
Location: Las Vegas
Posts: 6,277
labrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatness
 



Child Abuse Child Abuse
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.
__________________
:$: Support Forum <-- My latest endeavor.:loveyou:
Debate Forums Free Online Sudoku My vBum Blog
labrocca is offline  
Old 03-05-2007, 08:55 PM THREAD STARTER               #13 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
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.
mholt is offline  
Old 03-05-2007, 11:59 PM   #14 (permalink)
NamePros Expert
 
labrocca's Avatar
Join Date: Aug 2004
Location: Las Vegas
Posts: 6,277
labrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatnesslabrocca Has achieved greatness
 



Child Abuse Child Abuse
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.
__________________
:$: Support Forum <-- My latest endeavor.:loveyou:
Debate Forums Free Online Sudoku My vBum Blog
labrocca is offline  
Old 03-06-2007, 01:21 AM   #15 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
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.)
cef is offline  
Old 03-06-2007, 05:31 PM THREAD STARTER               #16 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
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.
mholt is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:42 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger