Anush Posted June 11, 2014 Share Posted June 11, 2014 (edited) I am using a page to upload an image. Here are the code. This is working. My problem is when the user login and when there is session variables this is not working. please help. error is Notice: Undefined index: uploaded Main page <?php session_start(); if($_FILES['uploaded']['name']!== ''){ $target = "images/books/"; include("upload_image.php"); //Rename image $original_name=basename($_POST['uploaded']['name']); if($upload_file==1){ rename("images/books/$original_name","images/books/$book_id.jpg"); } ?> echo "<form action='' method='POST' enctype='multipart/form-data'><table cellpadding='2' cellspacing='2' border='0'>"; echo "<tr><td></td><td><input class='input-file uniform_on' type='file' name='uploaded' /></td>"; echo "</table></form>"; upload_image.php <?php $target = $target . basename($_POST['uploaded']['name']) ; $upload_file=0; $Err_count=0; if (filesize($_POST['uploaded']['tmp_name']) < 300000){ //300KB if(($_FILES['uploaded']['type'] == 'image/jpg') || ($_FILES['uploaded']['type'] == 'image/jpeg')){ if(is_uploaded_file($_POST['uploaded']['tmp_name'], $target)) { $upload_file=1; $Err_count=0; Info ("The file ". basename( $_POST['uploaded']['name']). " has been uploaded and renamed to relevant image file"); } else { Error ("Sorry, there was a problem uploading your file"); $Err_count=1; } } else{ $Err_count=2; Error ("Your file is not an image of jpg or jpeg"); } } else{ $Err_count=3; Error ("Your file is too large. Please upload less than 300KB"); } ?> Edited June 11, 2014 by Anush Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 11, 2014 Share Posted June 11, 2014 The error happens if you call the script without uploading a file, because you incorrectly assume that $_FILES['uploaded']['name'] is always defined. Maybe you actually meant to check for isset($_FILES['uploaded'])? Quote Link to comment Share on other sites More sharing options...
Anush Posted June 11, 2014 Author Share Posted June 11, 2014 Thanks Jacques1. But problem not solved. I changed the code. here it is. if(isset($_FILES['uploaded'])){ if($_FILES['uploaded']['name']!== ''){ echo $_FILES['uploaded']['name']; $target = "images/books/"; include("upload_image.php"); //Rename image $original_name=basename($_FILES['uploaded']['name']); if($upload_file==1){ rename("images/books/$original_name","images/books/$book_id.jpg"); } } } No errors appear. but file upload not happen. no uotput from follwing code also. echo $_FILES['uploaded']['name']; Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 11, 2014 Share Posted June 11, 2014 after you have detected that a post method form has been submitted ($_SERVER['REQUEST_METHOD'] will be equal to 'POST'), the $_FILES array will be empty if the size of the uploaded file exceeds the post max size setting. also, if the $_FILES array is not empty, i.e. your expected $_FILES['uploaded'] element is set, you must test if the upload worked correctly (the $_FILES['uploaded']['error'] element is a zero), before you can use any of the uploaded file information. to detect and report back to the user which of these conditions has caused the upload to fail, you would need to write program logic to do so. Quote Link to comment Share on other sites More sharing options...
Anush Posted June 11, 2014 Author Share Posted June 11, 2014 when a file is set to upload if(isset($_POST['uploaded'])){ echo $_POST['uploaded']; } Output====> sdfdf.jpg if(isset($_FILES['uploaded'])){ echo $_FILES['uploaded']; } No output Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted June 11, 2014 Share Posted June 11, 2014 (edited) You've gotta be kiding, right? Is it your html upload files form? How many forms you have in your html document? echo "<form action='' method='POST' enctype='multipart/form-data'><table cellpadding='2' cellspacing='2' border='0'>"; echo "<tr><td></td><td><input class='input-file uniform_on' type='file' name='uploaded' /></td>"; echo "</table></form>"; Edited June 11, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
Anush Posted June 12, 2014 Author Share Posted June 12, 2014 one form upload both data and files Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 12, 2014 Share Posted June 12, 2014 The symptoms you describe (getting the filename as a POST parameter rather than the actual file) occur when you screw up the enctype. Make a minimal example with nothing but a form and a script which inspects the incoming data: <?php echo 'Content of $_FILES:<br>'; var_dump($_FILES); echo 'Content of $_POST:<br>'; var_dump($_POST); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Upload</title> </head> <body> <form method="post" enctype="multipart/form-data"> <input type="file" name="test_file"> <input type="submit"> </form> </body> </html> So what happens when you submit this form? Quote Link to comment Share on other sites More sharing options...
Anush Posted June 13, 2014 Author Share Posted June 13, 2014 Here is the out put when no session is there in my site. Content of $_FILES:array(1) { ["test_file"]=> array(5) { ["name"]=> string(9) "hotel.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(24) "C:\xampp\tmp\php9B8B.tmp" ["error"]=> int(0) ["size"]=> int(99554) } }Content of $_POST: array(0) { } Here is the out put when session is there in my site. Content of $_FILES:array(0) { }Content of $_POST: array(1) { ["test_file"]=> string(9) "hotel.jpg" } Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted June 13, 2014 Share Posted June 13, 2014 Are you still getting this notice? Undefined index: uploaded Why don't you try to show us all relevant code and entire html uploading form? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 13, 2014 Share Posted June 13, 2014 a symptom of variables changing value/having unexpected values in them, that are not due to program logic errors, is usually due to php's register_globals being on and having same names for $_GET, $_POST, $_COOKIE, $_FILES, $_SESSION variables. what php version are you using and is the register_globals setting ON? Quote Link to comment Share on other sites More sharing options...
Anush Posted June 15, 2014 Author Share Posted June 15, 2014 register_globals setting OFF. PHP Version 5.3.5 This is my code <?phpecho 'Content of $_FILES:<br>';var_dump($_FILES);echo 'Content of $_POST:<br>';var_dump($_POST);?><!DOCTYPE html><html><head><meta charset="utf-8"><title>Upload</title></head><body><form method="post" enctype="multipart/form-data"><input type="file" name="test_file"><input type="submit"></form></body></html> Here is the out put when no session is there in my site. Content of $_FILES:array(1) { ["test_file"]=> array(5) { ["name"]=> string(9) "hotel.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(24) "C:\xampp\tmp\php9B8B.tmp" ["error"]=> int(0) ["size"]=> int(99554) } }Content of $_POST: array(0) { } Here is the out put when session is there in my site. Content of $_FILES:array(0) { }Content of $_POST: array(1) { ["test_file"]=> string(9) "hotel.jpg" } Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted June 15, 2014 Share Posted June 15, 2014 Then, you have errors somewhere in your script when using sessions. Add the following code on the top of this file and please, use the forum's code [ code ] tags when posting code. <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); ini_set('output_buffering', 0); error_reporting(-1) echo 'Content of $_FILES:<br>'; var_dump($_FILES); echo 'Content of $_POST:<br>'; var_dump($_POST); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Upload</title> </head> <body> <form method="post" enctype="multipart/form-data"> <input type="file" name="test_file"> <input type="submit"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Anush Posted June 15, 2014 Author Share Posted June 15, 2014 Thanks jazzman1. But nothing display. same scenario happen. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 15, 2014 Share Posted June 15, 2014 the symptom is that of a form tag that doesn't have the enctype attribute in it. it's likely that you have multiple forms on the page and are ending up with an extra opening <form ...> tag or a missing closing </form> tag, resulting in your upload form being nested inside of a non-upload form. you would need to post your complete code that's producing the page with the form. the snippets of code you have been posting don't show anything to do with code using session variables and aren't the full story. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 16, 2014 Share Posted June 16, 2014 Anush, you keep talking about different results depending on whether "there's a session on my site". I have no idea what that means. The example code I gave you doesn't involve any sessions at all, so you've obviously changed something. What did you change? What do you mean by "session on my site"? Quote Link to comment Share on other sites More sharing options...
Anush Posted June 16, 2014 Author Share Posted June 16, 2014 Hi Jacques1, That code is working. I use that code as a new page in my site. Actual page that i am going to use is checking whether user is logged in. So i used ur code when user is logged in. Then it is not working. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted June 18, 2014 Share Posted June 18, 2014 So i used ur code when user is logged in. Then it is not working. Interesting Can you prove it? Post the script when the user is on a logged in session? Quote Link to comment 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.