jasonc Posted June 8, 2006 Share Posted June 8, 2006 trying to add an upload photo to the site, button.can someone please suggest the best and yet secure way i can do this.i mostly use PHP so if it can be done in PHp thats great.I have once seen a form based upload image version but can not seem to find it.any ideas?thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/ Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 you could try [a href=\"http://www.phpfreaks.com/tutorials/85/0.php\" target=\"_blank\"]this[/a] tutorial or just search through the tutorials section, there are definitely ones in there Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43457 Share on other sites More sharing options...
jasonc Posted June 9, 2006 Author Share Posted June 9, 2006 after searching google i found.... [a href=\"http://www.blazonry.com/scripting/upload-size.phps\" target=\"_blank\"]http://www.blazonry.com/scripting/upload-size.phps[/a]just added this script to a new php file, and viewed it and tried to upload a file to my server, nothing worked.i changed the $uploaddir = ".";to read the FULL server path to the folder i wish to store the files in.still thinki think i must be missing something, have i missed something out?[!--quoteo(post=381667:date=Jun 9 2006, 01:58 AM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 01:58 AM) [snapback]381667[/snapback][/div][div class=\'quotemain\'][!--quotec--]you could try [a href=\"http://www.phpfreaks.com/tutorials/85/0.php\" target=\"_blank\"]this[/a] tutorial or just search through the tutorials section, there are definitely ones in there[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43463 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 could you link me to your script? Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43466 Share on other sites More sharing options...
jasonc Posted June 9, 2006 Author Share Posted June 9, 2006 [a href=\"http://www.pro-freelancers.com/\" target=\"_blank\"]http://www.pro-freelancers.com/[/a][!--quoteo(post=381676:date=Jun 9 2006, 02:26 AM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 02:26 AM) [snapback]381676[/snapback][/div][div class=\'quotemain\'][!--quotec--]could you link me to your script?[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43469 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 Yup, I see your problem. In the <form> tags, you need to put the name of the PHP script used to upload the file in the action element. Like [code]<form action="script.php" name="a_form" method="post">[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43473 Share on other sites More sharing options...
jasonc Posted June 9, 2006 Author Share Posted June 9, 2006 i know that but i have not placed anything here before as it goes back to the same script, or have i missed a script out and there is more to this than i thought?[!--quoteo(post=381683:date=Jun 9 2006, 02:43 AM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 02:43 AM) [snapback]381683[/snapback][/div][div class=\'quotemain\'][!--quotec--]Yup, I see your problem. In the <form> tags, you need to put the name of the PHP script used to upload the file in the action element. Like [code]<form action="script.php" name="a_form" method="post">[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43475 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 even though it goes back to the same script you still have to put the script name in there. So say if your script was called script.php which generated that form, in the action element you would put script.php (eg action="script.php"). That way, what happens is the browser leaves your page and follows the action which happens to be pointing to the same script. So then it loads up the script again but this time it has all the form data, which the newly loaded script detects and uploads. Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43483 Share on other sites More sharing options...
jasonc Posted June 9, 2006 Author Share Posted June 9, 2006 yes i see, but still when i change it it still does not upload.[!--quoteo(post=381693:date=Jun 9 2006, 03:03 AM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 03:03 AM) [snapback]381693[/snapback][/div][div class=\'quotemain\'][!--quotec--]even though it goes back to the same script you still have to put the script name in there. So say if your script was called script.php which generated that form, in the action element you would put script.php (eg action="script.php"). That way, what happens is the browser leaves your page and follows the action which happens to be pointing to the same script. So then it loads up the script again but this time it has all the form data, which the newly loaded script detects and uploads.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43485 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 OK, try changing [code]if ($REQUEST_METHOD == "POST")[/code] to [code]if(isset($_REQUEST['submit']))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43488 Share on other sites More sharing options...
jasonc Posted June 9, 2006 Author Share Posted June 9, 2006 ERRORImage Extension Unknown.Please upload only a JPEG image with the extension .jpg or .jpeg ONLYThe file you uploaded had the following extension:Warning: unlink(): No such file or directory in /home/profree/public_html/index.php on line 103[!--quoteo(post=381698:date=Jun 9 2006, 03:14 AM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 03:14 AM) [snapback]381698[/snapback][/div][div class=\'quotemain\'][!--quotec--]OK, try changing [code]if ($REQUEST_METHOD == "POST")[/code] to [code]if(isset($_REQUEST['submit']))[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43491 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 OK, I've been looking through the script and it doesn't look very good. So here's my verson (does not include the HTML, you can use the same as before, except you need to make sure the MAX_FILE_SIZE bit goes before the file input):[code]if(isset($_REQUEST['submit'](({$file = $_FILES['imgfile']['name'];$tmp_file = $_FILES['imgfile']['tmp_name'];$size = $_FILES['imgfile']['size'];$uploaddir = '.';$upload = $uploaddir . basename($file);$allowed = array('jpg', 'jpeg', 'gif', 'png');$ext = getFileExtension($file);if(!in_array($ext, $allowed)){echo "ERROR: Invalid file extension.";unlink($tmp_file);exit;}if(move_uploaded_file($tmp_file, $upload)){echo "Image uploaded successfully!";}else{echo "Image uplaod failed.";}// Same function as the other scriptfunction getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }}[/code]It's pretty insecure but it'll get the job done. Quote Link to comment https://forums.phpfreaks.com/topic/11540-upload-photo-script/#findComment-43498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.