Uploading a file

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Flux
-


Joined: 13 Oct 2006
Posts: 48

PostPosted: Mon Feb 12, 2007 9:35 pm    Post subject: Uploading a file Reply with quote

My file upload script works fine, except for one thing.
It's the only script in my arsenal that cannot read $_SESSION['s_username'] (as it is defined on my website, anyway) and that's critical for deciding where the file will go.
When a user uploads a file to my server, they need to be logged in so that it works.
The script that handles where the upload will go decides the location by interpreting the string:
"/files/" . $_SESSION['s_username'] . "/" . $medium . "/" . $_FILES['file']['name']
Where $medium is defined as either music, pics, video, or other, depending on the output of an if/elseif/elseif/else statement that deciphers the file type.

So, if a member by the username "joe" decides to upload "this.jpg" it should go to /files/joe/pics/this.jpg

But my upload script can't, as stated above, read $_SESSION['s_username'] so it tries to save it as /files//pics/this.jpg

It also needs to read the session information to determine if a file by the same name in the same folder already exists.

Here's the script in full, not including the html form, located on another page:
Code:
<?php
if ((!isset($_SESSION['s_logged_n'])) || (!isset($_SESSION['s_username'])))
{
echo '<script>alert("Error:  The script cannot identify a username.");</script>';
echo '<script>history.back(1);</script>';
}
else
{
if ($_FILES['file']['size'] < 20971520)
  {
  if ($_FILES['file']['error'] > 0)
    {
    echo "Error: " . $_FILES['file']['error'] . "<br />";
    }
  else
    {
   if (($_FILES['file']['type'] == "image/gif") OR ($_FILES['file']['type'] == "image/jpeg") OR ($_FILES['file']['type'] == "image/pjpeg") OR ($_FILES['file']['type'] == "image/png") OR ($_FILES['file']['type'] == "image/bmp") OR ($_FILES['file']['type'] == "image/x-emf")) {
   $medium = "pics";
   }
   elseif (($_FILES['file']['type'] == "audio/mpeg") OR ($_FILES['file']['type'] == "audio/x-ms-wma") OR ($_FILES['file']['type'] == "audio/wav")) {
   $medium = "music";
   }
   elseif (($_FILES['file']['type'] == "video/avi") OR ($_FILES['file']['type'] == "video/x-ms-wmv") OR ($_FILES['file']['type'] == "video/quicktime")) {
   $medium = "video";
   }
   else {
   $medium = "other";
   }
    if (file_exists("files/" . $_SESSION['s_username'] . "/" . $medium . "/" . $_FILES['file']['name']))
      {
      echo $_FILES["file"]["name"] . " already exists.  You should rename it.";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "files/" . $_SESSION['s_username'] . "/" . $medium . "/" . $_FILES['file']['name']);
     echo "Name: " . $_FILES['file']['name'] . "<br />";
      echo "Type: " . $_FILES['file']['type'] . "<br />";
      echo "Size: " . ($_FILES['file']['size'] / 1024) . " KB<br />";
      echo "Stored in: " . "/files/" . $_SESSION['s_username'] . "/" . $medium . "/" . $_FILES['file']['name'];
      }
    }
  }
else
  {
  echo 'Your file is bigger than 20 MB.  That is, it's too big.';
  }
}
?>


So, I applied a failsafe, as you can see, at the top. As a result, all I get is a message window saying "Error: The script cannot identify a username."

Why is this script unable to read the session info?
(By the way, I have checked my login.php file to make sure that it defines that session information that is required, and as i said before, this upload.php script is the ONLY script that can't read it.)

Any help is as always appreciated.
_________________
My signature is so lame.

I think phpBB forums hates my laptop '.';
Back to top View user's profile Send private message
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Mon Feb 12, 2007 9:58 pm    Post subject: Reply with quote

you seem to have forgotten to include the session_start(); function.
_________________
Olly
Back to top View user's profile Send private message
Flux
-


Joined: 13 Oct 2006
Posts: 48

PostPosted: Mon Feb 12, 2007 10:07 pm    Post subject: Reply with quote

shoot....
Is that it?!
sigh...
Sorry for bothering everyone..
And thanks olly86
_________________
My signature is so lame.

I think phpBB forums hates my laptop '.';
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group