Jump to content

sessions with $_FILE


Anush

Recommended Posts

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 by Anush
Link to comment
Share on other sites

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'];
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by jazzman1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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" }                          

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

register_globals setting OFF.

 

PHP Version 5.3.5

 

This is my code

 

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

 

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" }                          

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.