Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
php error not working
Old 11-14-2008, 11:35 PM php error not working
Registered User

Posts: 78
Name: Joseph
Location: Texas
Trades: 0
I have a php image upload form that works perfect but my only problem is that when an image over the set limit is uploaded it says it has exceeded the size and it doesnt give the link to the image but it still uploads it to my server.

Here is the code:

PHP Code:

<?php
//define a maxim size for the uploaded images in Kb
 
define ("MAX_SIZE","1024"); 

//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'];
     
//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
             
echo '<h3>Unknown extension!</h3>';
             
$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*1048576)
{
    echo 
'<h3>You have exceeded the size limit!</h3>';
    
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$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
{
    echo 
'<h3>Copy unsuccessfull!</h3>';
    
$errors=1;
}}}}

//If no errors registred, print the success message
 
if(isset($_POST['Submit']) && !$errors
 {
     echo 
"<h3>File Uploaded Successfully!</h3>";
 }

 
?>
Thanks
josephcohen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-15-2008, 01:58 AM Re: php error not working
Super Talker

Posts: 102
Trades: 0
Did you make this, if so nice on the comments. I can't really help tho I just wanted to ask that.
Aaron™ is offline
Reply With Quote
View Public Profile
 
Old 11-15-2008, 06:21 AM Re: php error not working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
but my only problem is that when an image over the set limit is uploaded it says it has exceeded the size and it doesnt give the link to the image but it still uploads it to my server.
Because PHP doesn't know it is too big until it is uploaded.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-15-2008, 11:24 AM Re: php error not working
Registered User

Posts: 78
Name: Joseph
Location: Texas
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Because PHP doesn't know it is too big until it is uploaded.
Thats what i thought.

Well how can i CHANGE that!

Do I just need to change the positioning of stuff?
josephcohen is offline
Reply With Quote
View Public Profile
 
Old 11-15-2008, 02:11 PM Re: php error not working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You can't

PHP runs on the server and until the file is actually on the server it can't do anything about it.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-15-2008, 11:55 PM Re: php error not working
Experienced Talker

Posts: 41
Name: Jabis Sevon
Location: Tampere, Finland
Trades: 0
Just unlink the pic after the error

PHP Code:

<?php
//define a maxim size for the uploaded images in Kb
 
define ("MAX_SIZE","1024"); 

//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'];
     
//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
             
echo '<h3>Unknown extension!</h3>';
             
$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*1048576)
{
    echo 
'<h3>You have exceeded the size limit!</h3>';
    
unlink($_FILES['image']['tmp_name']); //ADDED LINE
    
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$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
{
    echo 
'<h3>Copy unsuccessfull!</h3>';
    
$errors=1;
}}}}

//If no errors registred, print the success message
 
if(isset($_POST['Submit']) && !$errors
 {
     echo 
"<h3>File Uploaded Successfully!</h3>";
 }

 
?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
jabis is offline
Reply With Quote
View Public Profile Visit jabis's homepage!
 
Old 11-16-2008, 05:34 AM Re: php error not working
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
If you did not move or copy the uploaded file to somewhere you will not have to unlink it explicitly because it will be deleted from tmp right after your script ends.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 11-16-2008, 02:51 PM Re: php error not working
Registered User

Posts: 78
Name: Joseph
Location: Texas
Trades: 0
Thanks jabis

It looks like the code will work.

And mtishetsky i dont understand what ur saying?

I have to copy the file to somewhere cause after the image is uploaded i give the user a link to the image uploaded.
josephcohen is offline
Reply With Quote
View Public Profile
 
Old 11-16-2008, 03:10 PM Re: php error not working
Experienced Talker

Posts: 41
Name: Jabis Sevon
Location: Tampere, Finland
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
If you did not move or copy the uploaded file to somewhere you will not have to unlink it explicitly because it will be deleted from tmp right after your script ends.
I was merely pointing out a simple solution, eventhough a bit hackish, but I expected the result to be

You have exceeded the size limit!
Copy Unsuccesful!

and then exit, so that there wouldn't be need to refactor much of the code... There'd be many more elegant solutions available, but this was a simple and fast hack to get the desired result.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
jabis is offline
Reply With Quote
View Public Profile Visit jabis's homepage!
 
Old 11-16-2008, 04:11 PM Re: php error not working
Registered User

Posts: 78
Name: Joseph
Location: Texas
Trades: 0
Quote:
Originally Posted by jabis View Post
I was merely pointing out a simple solution, eventhough a bit hackish, but I expected the result to be

You have exceeded the size limit!
Copy Unsuccesful!

and then exit, so that there wouldn't be need to refactor much of the code... There'd be many more elegant solutions available, but this was a simple and fast hack to get the desired result.
well it worked so i guess its ok.
josephcohen is offline
Reply With Quote
View Public Profile
 
Old 11-16-2008, 04:46 PM Re: php error not working
Experienced Talker

Posts: 41
Name: Jabis Sevon
Location: Tampere, Finland
Trades: 0
Quote:
Originally Posted by josephcohen View Post
well it worked so i guess its ok.
Well surely it will work, but the script isn't as optimized it could be, because now it's intentionally trying to copy a non-existent file. You might want to check your error-handling later on when you've more knowledge on what the best practices are etc, but for now, it'll give you your desired effect to this extent

Glad to've been of assistance again!
-Jabis out-
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
jabis is offline
Reply With Quote
View Public Profile Visit jabis's homepage!
 
Reply     « Reply to php error not working
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55461 seconds with 12 queries