Jump to content

[SOLVED] Is this too hard for a noob to make work?


justAnoob

Recommended Posts

Well my upload scipt is woking. I know I still have more work to do with it, but for right now, It Works!(Yippee). How difficult would it be to link the username of the person signed in, with the files that he/she uploaded. Right now, I can go look in the database and see a bunch of pictures, but have no idea who put them there. Is this too hard to accomplish? Here is the upload script so far.

<?php
session_start();
include ("upload_db_info.php");

if (!empty($_POST['upload']))
{
    extract($_POST);
   	
    if(isset($_POST['upload']) && $_FILES['upload_file']['size'] > 0)
    {
        $fileName = $_FILES['upload_file']['name'];
        $tmpName = $_FILES['upload_file']['tmp_name'];
        $fileSize = $_FILES['upload_file']['size'];
        $fileType = $_FILES['upload_file']['type'];
        if ( file_exists($tmpName) )
        {
           $content = file_get_contents($tmpName);
        }
    }
    else
    {
        unset($_SESSION['uploadcomplete']);
        $_SESSION['uploaderror'] = "Please select a picture.";
        header("location: http://www.----------.com");
        exit();
    }

    $fileName = mysql_real_escape_string($fileName);
    $fileSize = (int)$fileSize;
    $fileType = mysql_real_escape_string($fileType);
    $content  = mysql_real_escape_string($content);
    
    $query = "INSERT INTO UploadedFiles (name, size, type, content)VALUES
    ('$fileName', '$fileSize', '$fileType', '$content')";
    $result = mysql_query($query)or die (mysql_error());
    
    unset($_SESSION['uploaderror']);
    $_SESSION['uploadcomplete'] = "Your picture was uploaded to our
    system.";
    header("location: http://www.--------.com");
    exit();
}
?>

No, it's not hard. When you upload the file just get the userid from the session login (or wherever you have stored it) and insert it into a third table that contains the ids for the inserted images and the userid's.

 

If you have no idea what i just said then maybe a 101 in db design would help you understand the structure, Google is your friend ;)

Well, first of all. Am I on the right track? I created a session variable from my username variable. So when the user logs in, a new session is set. (id)

<?php
$_SESSION['id'] = "$username";
?>

Then in my upload script I added the following lines that are highlighted.

<?php
session_start();
include ("upload_db_info.php");

if (!empty($_POST['upload']))
{
    extract($_POST);
   	
    if(isset($_POST['upload']) && $_FILES['upload_file']['size'] > 0)
    {
        $user = $_SESSION['id'];      ///////////////////////////////////
        $fileName = $_FILES['upload_file']['name'];
        $tmpName = $_FILES['upload_file']['tmp_name'];
        $fileSize = $_FILES['upload_file']['size'];
        $fileType = $_FILES['upload_file']['type'];
        
        if ( file_exists($tmpName) )
       {
    	$content = file_get_contents($tmpName);
       }
    }
    else
    {
         unset($_SESSION['uploadcomplete']);
         $_SESSION['uploaderror'] = "Please select a picture.";
         header("location: http://www.--------.com");
         exit();
    }

    $user = mysql_real_escape_string($user);      ////////////////////////////
    $fileName = mysql_real_escape_string($fileName);
    $fileSize = (int)$fileSize;
    $fileType = mysql_real_escape_string($fileType);
    $content  = mysql_real_escape_string($content);
    
    $query = "INSERT INTO UploadedFiles (name, size, type, content, user)
    VALUES
('$fileName', '$fileSize', '$fileType',  '$content', '$user')";        ///////////////////////////added user and $user
    $result = mysql_query($query)or die (mysql_error());
    
unset($_SESSION['uploaderror']);
$_SESSION['uploadcomplete'] = "Your picture was uploaded to our system.";
    header("location: http://www.--------.com");
    exit();
}
?>

I do not get any errors but the user field in my database is not being filled with the username.

you are not setting the $user variable.  You set $_SESSION['id'] to $username, try changing $user to $username and see what that gets you

 

I see you are setting $user with $user

 

$user = mysql_real_escape_string($user)

 

should it be

 

$user = mysql_real_escape_string($username)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.