[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 03-03-2007, 01:01 PM   #1 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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);

    }

    
/*== 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.)
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-03-2007, 01:08 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 01:10 PM   #3 (permalink)
Eating Pie
 
iNod's Avatar
 
Join Date: Nov 2004
Location: Canada
Posts: 2,289
126.05 NP$ (Donate)

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, 01:15 PM   #4 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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)...
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-04-2007, 11:39 AM   #5 (permalink)
NamePros Regular
 
smilgy's Avatar
 
Join Date: Jan 2007
Posts: 253
29.20 NP$ (Donate)

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, 11:52 AM   #6 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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? :-/
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-04-2007, 01:42 PM   #7 (permalink)
NamePros Member
 
Join Date: Jan 2005
Posts: 112
0.00 NP$ (Donate)

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);
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, 03:44 PM   #8 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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(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... So um, be nice

Thanks! Help appreciated. Rep +
.chulium.
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-04-2007, 03:58 PM   #9 (permalink)
NamePros Member
 
Join Date: Jan 2005
Posts: 112
0.00 NP$ (Donate)

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, 04:44 PM   #10 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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.
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-05-2007, 04:43 PM   #11 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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?
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-05-2007, 06:41 PM   #12 (permalink)
Recovering Jerk

Forum Moderator

 
labrocca's Avatar
 
Join Date: Aug 2004
Location: Las Vegas
Posts: 5,499
1,196.64 NP$ (Donate)

labrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond repute

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.
labrocca is offline  
Old 03-05-2007, 07:55 PM   #13 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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.
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 03-05-2007, 10:59 PM   #14 (permalink)
Recovering Jerk

Forum Moderator

 
labrocca's Avatar
 
Join Date: Aug 2004
Location: Las Vegas
Posts: 5,499
1,196.64 NP$ (Donate)

labrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond reputelabrocca has a reputation beyond repute

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.
labrocca is offline  
Old 03-06-2007, 12:21 AM   #15 (permalink)
cef
NamePros Regular
 
Join Date: May 2004
Location: NYC
Posts: 236
76.50 NP$ (Donate)

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, 04:31 PM   #16 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

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.
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Closed Thread


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 10:03 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85