amorphous4u Posted June 13, 2009 Share Posted June 13, 2009 Hi, I am trying to upload an image to my server using below php code: html <td width="116"><FORM ENCTYPE="multipart/form-data" ACTION="_URL_" METHOD=POST>Upload your photo: <td width="156"><INPUT NAME="userfile" TYPE="file"></td> <?php $userfile = $_POST['userfile']; echo "1".$userfile; echo "2".$_FILES['$userfile']; echo "3".$_FILES['userfile']; if((!empty($_FILES["userfile"])) && ($_FILES['userfile']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['userfile']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["userfile"]["type"] == "image/jpeg") && ($_FILES["userfile"]["size"] < 350000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['userfile']['tmp_name'],$newname))) { echo "It's done! The file has been saved as: ".$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["userfile"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 350Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; echo "4".$filename; } ?> I am getting output as: 1C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.jpg 2 3 Error: No file uploaded 4 It seems life $_FILES['$userfile'] is not capturing any value. Please help me. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/ Share on other sites More sharing options...
MadTechie Posted June 13, 2009 Share Posted June 13, 2009 The results seam kinda weird as $userfile = $_POST['userfile']; echo "1".$userfile; shouldn't give you the file path! try this <FORM ENCTYPE="multipart/form-data" ACTION="" METHOD=POST>Upload your photo: <INPUT NAME="userfile" TYPE="file"> <input type="submit"> </form> <?php $userfile = $_FILES['userfile']['name']; echo "1".$userfile; if((!empty($_FILES["userfile"])) && ($_FILES['userfile']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['userfile']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["userfile"]["type"] == "image/jpeg") && ($_FILES["userfile"]["size"] < 350000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['userfile']['tmp_name'],$newname))) { echo "It's done! The file has been saved as: ".$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["userfile"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 350Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; echo "2".$filename; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855148 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 Hi, I tried using your code and it is still now working. I got output as 1Error: No file uploaded2 Regards, Amit Verma Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855275 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 turn on error_reporting, error_reporting(E_ALL); did you submit the form? Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855278 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 Yes i did. And thats what i got the output. There are some sql insert queries also on that page and the data is getting inserted in tables successfully. But image is not getting uploaded. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855280 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 So did you try the code above in its own file, ? also add the error reporting Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855284 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 I aaded error reporting and getting these messages. Notice: Undefined index: music in /home/himachal/public_html/******.php on line 53 Notice: Undefined index: accepted in /home/himachal/public_html/******.php on line 87 Notice: Undefined variable: i in /home/himachal/public_html/******.php on line 159 1C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg Notice: Undefined index: userfile in /home/himachal/public_html/******.php on line 276 2Error: No file uploaded Notice: Undefined variable: filename in /home/himachal/public_html/******.php on line 302 3 Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855286 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 please read my last post! EDIT: infact let me be clear.. have you tried the code i posted in its own file with error reporting added ? Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855288 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 Yes and below is what i am getting on the output page. 1Error: No file uploaded Notice: Undefined variable: filename in /home/himachal/public_html/registerfirst.php on line 5 3 on line 5 code is $userfile = $_FILES['userfile']['name']; Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855291 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 I am very new to PHP and trying my best to learn it asap. I apologize if u are getting irritated by my silly questions. Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855293 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 The only way I can re-create the error is by submitting without selecting for a file first, check your php.ini file and check file_uploads is on upload_tmp_dir and upload_max_filesize are set correctly Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855294 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 Hi, I checked for my php.ini file_uploads is on upload_tmp_dir is set to /tmp and upload_max_filesize = 50 M what could be the possible reason of submitting this without selecting for a file and how can i find it? Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855300 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 I even added a variable to see if the file path is getting captured This is the output i am getting now C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg Notice: Undefined index: userfile in /home/himachal/public_html/registerfirst.php on line 7 1Error: No file uploaded Notice: Undefined variable: filename in /home/himachal/public_html/registerfirst.php on line 33 2 Please help me I am stuck with the last feature of my registration form. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855303 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 Please use the following code for debugging - echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; What does the above show AFTER the form has been submitted? Edit: And you should check the actual values of the settings using a phpinfo(); statement, because there is always a chance that the php.ini that you are examining is not the one that php is using. Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855305 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 the code you posted does have problems but the results you say your getting don't match, hence giving you some other code to test with, however that seams to be failing so either something very strange is going on or I'm not getting the whole story! Here is a very basic version, if this failed it should at least let us know why! with some details <?php if(isset($_POST['submit'])) { echo "<pre>"; var_dump($_FILES["userfile"]); echo "</pre>"; if($_FILES["userfile"]["error"] == 0) { if(move_uploaded_file($_FILES["userfile"]["tmp_name"],$_FILES["userfile"]["name"]) or die("Problems with upload")) { echo "Upload: {$_FILES["userfile"]["name"]}"; }else{ echo "FAILED: {$_FILES["userfile"]["name"]}"; } }else{ echo "error: ".$_FILES["userfile"]["error"]; } } ?> <form action="" method="post" enctype="multipart/form-data"> <input name="userfile" type="file" /><br /> <input type="submit" name="submit" value="Send files" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855308 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 Hi, This is what i am getting after adding your code. POST:Array ( [membershiptype] => 1 [login] => time [password] => time [repassword] => [gender] => 1 [fname] => [mname] => [lname] => [fathername] => [profilecreatedby] => 1 [dob] => [birthHour] => 00 [birthMin] => 00 [birthSec] => 00 [pob] => [height] => 0 [weight] => 0 [complexion] => 0 [bloodgroup] => 1 [bodytype] => 1 [maritalstatus] => 6 [religion] => None [caste] => 1 [subcaste] => [gotra] => [maternalcaste] => [mothertongue] => [manglik] => 1 [horoscope] => 1 [nakshatra] => none [education] => [occupation] => [income] => 0 [hobbies] => [interests] => [music] => [reads] => [movies] => [sports] => [cuisine] => [dress] => [agefrom] => [ageto] => [p_maritalstatus] => 6 [heightfrom] => 0 [heightto] => 0 [p_complexion] => 7 [p_bodytype] => 1 [p_religion] => [p_mothertongue] => [p_education] => [p_occupation] => [p_city] => [p_state] => [p_country] => => [address] => [city] => [district] => [state] => [country] => [phone] => [mobile] => [agentid] => [aboutu] => [aboutfamily] => [userfile] => C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg [agreecheck] => on [action] => Submit! ) FILES:Array ( ) Notice: Undefined index: userfile in /home/himachal/public_html/registerfirst.php on line 283 1Error: No file uploaded Notice: Undefined variable: filename in /home/himachal/public_html/registerfirst.php on line 309 2 Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855310 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 The fact that the $_FILES array is empty but the $_POST array has values, means one or more of the following - 1) Uploads are not enabled on your server, 2) Your form is invalid to the point that the browser cannot figure it out after some point or that it is not a valid upload form, 3) There is no valid type="file" form field. Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855315 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 or file_uploads is off (check phpinfo();) EDIT: oh and check postmaxsize Can you try my script also what OS and browser are you running this on ? Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855317 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 I did wht u asked i ran the code u gave me and nothing happened. I checked the error log and got this [13-Jun-2009 20:54:22] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20060613/session.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20060613/session.so: cannot open shared object file: No such file or directory in Unknown on line 0 Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855319 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 Nothing ? you should get the errors above the form.. try a small file! Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855321 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 There have been at least two posts asking that you use a phpinfo(); statement to check values. Any chance of doing that? Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855322 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 I tried 28KB file and still nothing. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855323 Share on other sites More sharing options...
MadTechie Posted June 14, 2009 Share Posted June 14, 2009 Check list <?php phpinfo(); ?> [*]upload_max_filesize [*]memory_limit [*]max_execution_time [*]post_max_size [*]file_uploads Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855325 Share on other sites More sharing options...
amorphous4u Posted June 14, 2009 Author Share Posted June 14, 2009 Hi, These are the values: upload_max_filesize 50M 50M memory_limit 32M 32M max_execution_time 60 60 post_max_size 8M 8M file_uploads On On Regards, Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855348 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 I'm going to bet this is all due to invalid form(s), even when using the code that MadTechie posted (which apparently gave no results), which I'll guess was probably added to existing code in the file instead of being used by itself. Quote Link to comment https://forums.phpfreaks.com/topic/162060-_filesuserfile-is-displaying-no-value/#findComment-855349 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.