Jump to content

Insert and read file path in database


I-AM-OBODO

Recommended Posts

What you are asking is so general that I can only provide a general answer. You don't state how the images are getting created/put into that folder. Are you manually putting them there, is the user uploading them, or what?

 

I have to assume you have a database with a user table. Based upon your statement it seems there is only one photo per user. If you will only ever need one image (e.g. a profile picture) then you can add a field to the user table. If you may need additional images for each user then you would want a separate table to store them with a foreign key reference back to the user record. I will assume that you only need one image per user.

 

If you are having users upload photos, then you need to start with an upload process. There are plenty of script available if you do a search. In that process there will be a line of code to move the uploaded photo to where you will store it. In that process you specify the location and name of the photo. Since these are all in the same folder you will want to ensure the photos have a unique name - I would suggest using the user id or username in the name. So you might have some code such as:

$path = "images/users/";
$name = $userName . 'jpg'; //This assumes you know the username
$destination = $filePath . $fileName;
if(!move_uploaded_file($tempFile, $destination))
{
    echo "Error uploading file";
}
else
{
    $query = "UPDATE users SET profile_pic = '$fileName'";
    $result = mysql_query($query);
}

 

Now, when you need to display the image for the user you do a SELECT query using the user ID

$path = "images/users/";

$query = "SELECT profile_pic FROM users WHERE user_id = '$userID'";
$result = mysql_query($query);
$user = mysql_fetch_assoc($result);

echo "<img src='{$path}{$user['profile_pic']}'>";

 

That's all just mock code, but should give you an idea of where to start

Link to comment
Share on other sites

hi. thanks for the help. it's not an image that is being uploaded but an html file and the file is exported from a different application.

 

the admin export the file to a folder from the application, every user have their own file. I have created a login page where users will login and view their respective html file.

 

I am to write a code for the admin to insert the file path to database so that when users login, they see their respective pages.

 

process required

admin

1. upload files from application to folder (this is achieved)

2. inserts the file path to database

 

users

1. login to view page

2. click on navigation to view respective file. (have two links for two viewable files, have to a select to determine which file to view)

3. logs out

 

I have done the login and logout, but on the links, how can I do a select to view the file and how can I as the admin insert the file path to database

 

thanks

Link to comment
Share on other sites

Everything I stated above still applies. The only difference is that instead of putting the image into an IMG tag you would instead use  the page returned from the query to include() it for the user.

 

thanks man, but I really don't get you. the files are already in a folder. do I have to upload them again into another folder? the files are in html. I just need to find a way of getting the file name from the folder and insert it into the database and add a unique id for each.

Link to comment
Share on other sites

Everything I stated above still applies. The only difference is that instead of putting the image into an IMG tag you would instead use  the page returned from the query to include() it for the user.

 

thanks man, but I really don't get you. the files are already in a folder. do I have to upload them again into another folder? the files are in html. I just need to find a way of getting the file name from the folder and insert it into the database and add a unique id for each.

Link to comment
Share on other sites

Well, you can create a script to read the files in that folder and add records for each one into the database. But then either someone needs to manually put a reference in each record for the user that each belongs to or you need to build a form to do that. I have no idea what would work for you and am not going to take time to try and come up with a solution if the requirements aren't clear.

 

But, here is a mock script to read the files in the folder and generate an INSERT query. You could just run the script to generate the query and not run it. Then copy the query into a text editor to add any additional data for each record to create a complete records.

 

$file_path = "path/to/files/";

$files = glob($file_path . '*.htm');
$insertValues = array();
foreach($files as $fileName)
{
    $insertValues[] = "('{$fileName}')";
}

$query = "INSERT INTO files (`file_path`) VALUES " implode(', ', $insertValues);
echo $query;

Link to comment
Share on other sites

Hi,

Thanks for the help. I have decided that the file be uploaded from a form so that it

could be referenced. Below is my code for the upload. The upload is now working but my

problem now is on the user side. How can i retrieve the file from the database so that

it could be view. From what i did, i could view the file name but can not see its content.

My guess is, i am not directing to the path where the file is. Help needed pls

 

PS: If what i am doing is wron pls point me to the right way

 

Thanks

 

Admin

<?php
//directory to store files
$target = "test/";
$target = $target . basename( $_FILES['file_name']['name']);


$username=$_POST['username'];
$real_file=($_FILES['file_name']['name']);

if($username == ''){
echo 'Username empty<br>';
}
if($real_file == ''){
echo 'File field empty<br>';
}else{

// Connects to your Database
mysql_connect("localhost", "user", "pwd") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

//Writes the information to the database
mysql_query("INSERT INTO `client_file` VALUES ('', '$username', '$real_file')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['file_name']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['file_name']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
?>	

 

User Area

 

<?php

mysql_connect("localhost", "user", "paswod") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

$data = mysql_query("SELECT file_name FROM client_file WHERE username = 'john'") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data

Echo "<b>File Name:</b> ".$info['file_name'] . "<br> ";
}

?>

 

Link to comment
Share on other sites

Well, you can create a script to read the files in that folder and add records for each one into the database. But then either someone needs to manually put a reference in each record for the user that each belongs to or you need to build a form to do that. I have no idea what would work for you and am not going to take time to try and come up with a solution if the requirements aren't clear.

 

But, here is a mock script to read the files in the folder and generate an INSERT query. You could just run the script to generate the query and not run it. Then copy the query into a text editor to add any additional data for each record to create a complete records.

 

$file_path = "path/to/files/";

$files = glob($file_path . '*.htm');
$insertValues = array();
foreach($files as $fileName)
{
    $insertValues[] = "('{$fileName}')";
}

$query = "INSERT INTO files (`file_path`) VALUES " implode(', ', $insertValues);
echo $query;

Well, you can create a script to read the files in that folder and add records for each one into the database. But then either someone needs to manually put a reference in each record for the user that each belongs to or you need to build a form to do that. I have no idea what would work for you and am not going to take time to try and come up with a solution if the requirements aren't clear.

 

But, here is a mock script to read the files in the folder and generate an INSERT query. You could just run the script to generate the query and not run it. Then copy the query into a text editor to add any additional data for each record to create a complete records.

 

$file_path = "path/to/files/";

$files = glob($file_path . '*.htm');
$insertValues = array();
foreach($files as $fileName)
{
    $insertValues[] = "('{$fileName}')";
}

$query = "INSERT INTO files (`file_path`) VALUES " implode(', ', $insertValues);
echo $query;

 

hi. can you pls take a look at my last post?

thanks

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.