show the contents of a folder in a web page - php

I'm trying to build an online storage site similar to Google drive. My code can upload files and save it in a specific given path. It also has code for creating files. the only thing is missing is showing the user "how many" or "which" files he/she uploaded in the specific path.
As shown in the Google drive image (in the link below), I want my code to look like similar to this, I want my user to see he/she's uploaded files in a serial on the web page. I'm not sure how to do it or whether it is possible in php or not, or if it is possible in php then which function is needed. I'm also not sure "what to type" in the Google search to find some examples of this type of coding. please tell me anything that will help me to start this part of my project.
http://www.google.com.bd/imgres?sa=X&biw=1024&bih=605&tbm=isch&tbnid=kSX8G1DGHuYiHM:&imgrefurl=http://www.zdnet.com/blog/networking/free-storage-for-you-google-drive-to-arrive-today/2293&docid=VGLnSQuNf4vGLM&imgurl=http://www.zdnet.com/i/story/62/58/002293/google-drive.png&w=1012&h=725&ei=JPCNUtj_FoKtrAfYkoHwCA&zoom=1&ved=1t:3588,r:12,s:0,i:122&iact=rc&page=2&tbnh=173&tbnw=241&start=8&ndsp=14&tx=182&ty=107
(I'm still working offline. I haven't yet launched it online.)
if you need any code from my existing work please tell me. I would love to upload my code.
---thanks.

So, you can just insert records in the Database while uploading (saving/moving files).
And example pseudocode, as you didn't provide any, would be:
if (checkForErrors()) {
// your error page for file exists, improper extension, and other violated constraints
} else {
move_uploaded_file($_FILES['file']['tmp_name'], 'your/path/here/' . $_FILES['file']['name']);
$db->query("INSERT INTO uploads (uploader, filename, uploaded_on) VALUES ('{$_SESSION['username']}', '{$_FILES['file']['name']}', NOW());");
}
// later you can just fetch the results for the current uploaded
$db->query("SELECT filename, uploaded_on FROM uploads WHERE uploader = '{$_SESSION['username']}';");
// under the row of filename will be stored the files that the current user has uploaded

Related

How would my databse grab the files in my server?

So I was thinking and thinking and couldn't figure it out. So I was following this tutorial which I want to implement on my site https://www.w3schools.com/php/php_file_upload.asp and on the tutorial when the user uploads their image it sends it straight to a folder, which will obviously be in the server files. Now my problem here is, if I want to display their image somewhere on their profile when they upload it, how would I go about grabbing the image from the folder, or do I have to change the code on the tutorial so that it saves in my database? I really have no clue, and I am now officially lost and anxiety is slowly falling down my imaginary tears that are illustrated by my depressed brain, and yeah.
You save the images in a specific folder, then save the filename in the database. Whenever you want to retrieve this file in your front-end you just take the filename from the database and link to wherever you put the file.

Secure files and display them as well

My question is about HTML and PHP.
This is my setup right now:
A website where user have accounts
A FTP server with pictures (currently none)
Files are currently saved on the website in the "PICTURES" folder (which is accessible by everybody who know the full URL)
So, I would like to know how I can display the images without storing them on the website (which will fix my URL problem).
My idea was to move the files on the FTP server, and when a users logon and request a page with those images, download them through a FTP connection, save them on the website, display the images, and remove them. Which would make them accessible only between the downloading time. But this solutions sounds REALLY bad to me.
You need always to have a place where your images are stored. But, if you don't want to give a user the chance to know where are stored, you can create a system which is used to show the images.
Think about this, if you want to download a file from Mega, you can't access to the URL where the file is stored, instead of that, the server itselfs calls a system who assign you a "key" and you can download the file only through that system using your "key".
You could use a system like "base64" so you can encode your image, and show it using it, or, you can use the "header" modifier so, you can display an image using a PHP code.
For example your image tag will be like:
<img src="processImage.php?id=01&user=10&key=123" />
So, your processImage will return a "tricky" image, actually not the image, but the code processed by PHP will be returned, like using "imagejpg()" function with the header "Content-Type:image/jpeg" and then the user will not know where the image is stored actually but the img will works actually.

Want to store and retrive data in MySQL on different pages

I'm handling a project and from long time that project is in the middle. The problem is : to store and retrive contents from MySQL database (locally in WAMP Server).
The brief overview about the project which inculdes the folowing steps:
1) Users can create account,
2) Upload files (whcih can be text(.rtf & .txt), images (.jpg & .png ), docs (.doc, .xls, .pptx, .pdf ) & videos (.avi & .mp4 ))
3) View those files and
4) Modify them.
So the concept is :
1) User logs-in or do sign-up,
2) He gets options the following options : Text, Images, Docs & Videos.
3) The user choose any option : which opens a new page where user can view his/her existing content in Thumbnail view with its name and
4) There's an option on top of everything to "Add new". Here we've two conditions:
If user clicks on existing, user can view it and delete the existing files. For example, if it's a video, user can play it by clicking on thumbnail.
If user clicks on "Add new", it takes user to another page where user can upload the respective content.
So, the problem is that no one in my team (we're in college, not in a company) knows the queries to store the file into database & after uploading it, retrieve the file's name and thumbnail (only in case of image & video otherwise, a default thumbnail) on previous page.
One of my team member tried to do it, but the uploaded file is shown in every user's account.
So I want to know how we can do it. I mean to say, specific programming code for both problems.
P.S.: If possible please let me know how to set restrictions(validations) of file choosing (like during uploading FB cover, it shows only IMAGES not ALL FILES).
This should help you. But be sure to clean this code from sql injections.
//This is the directory where images will be saved
$target = "pics/";
$target = $target . basename( $_FILES['Filename']['name']);
//This gets all the other information from the form
$Filename=basename( $_FILES['Filename']['name']);
$Description=$_POST['Description'];
//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded, and your information has been added to the directory";
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("yourdbname") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO tbl_files (Filename,Description)
VALUES ('$Filename', '$Description')") ;
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
// Connect to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("dbname") or die(mysql_error()) ;
// directory where images will be moved
$file=mktime()."_".$_FILES['files']['name'];
$target = "images/" . $file;
//This gets all the other information from the form
$description=$_POST['Description'];
//Writes the Filename to the server
if(move_uploaded_file($_FILES['files']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file has been uploaded, and your information has been added to the directory";
//Writes the information to the database
mysql_query("INSERT INTO tbl_files (Filename,Description)
VALUES ('$file', '$description')") ;
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading the file.";
}
Your question is much too broad. Here are some tips. Come back with more specific questions after you have tried things.
"User" management is a non-trivial task. You need admin page(s) to create new users, delete users, etc. A "end-user" should not be a MySQL "user"; there should be only one or two MySQL users ("app-user"). The PHP code will validate an "end-user", using your own tables for storing their names, passwords, etc. The PHP code will connect to mysql as the app-user.
Media files should be stored on disk as files. This allows the easy use of HTML code to display images (<img...>) or play videos.
Thumbnails -- The simple answer is to treat them like other media. (In one project, I stored them in the db and clumsily converted them to base64 for inclusion in <img...>.)
Thumbnails -- There may be a thumbnail in the "exif data". But I would not trust it to exist. Instead, I would use image* routines in PHP to create a thumbnail from an image. (I do not know how to deal with thumbnails of videos.)
PHP has functions for uploading media. It is a bit clumsy and contorted; study the manual.
PHP is picky (for good security reasons) about access to the filesystem. You will need to poke holes (see open_basedir) to allow access.
You will need to pass information from one page to another. Keep in mind that HTML pages are "stateless", so nothing from one page is implicitly available to the next. You may be able to put everything in the url (...?user=123&img=abc...). But there could be security considerations. Or you may need to get "cookies" involved.
"The user clicks..." -- You may need JavaScript starting with OnClick=DoSomeFunction(...). You may need AJAX, but it does not sound like it.
Otherwise, HTML, PHP, and MySQL can handle everything you need.
Those notes probably encompass several thousand lines of PHP, including a several dozen SQL queries. Divvy up the project among your team so that not everyone has to be come versed in every aspect. Suggested breakdown: User management; Media processing; Flow of pages (clicks, passing args, security, etc); Database abstraction layer.

Could my file structure be causing the issues with my photo upload system?

I'm trying to create a photo upload system following Adam Khourys online tutorials.
http://www.developphp.com/view.php?tid=1395 - Upload multiple files video
http://www.developphp.com/view.php?tid=1304 - User profile upload
Most of the code is like for like, but with my own variable names used instead. One major difference is instead of creating 3 different file upload fields as in the multiple upload video I created my form to allow multiple selections from just one upload field.
Below is a brief overview of the files, their location and what the page does.
create_post.php located in root directory - the page that allows for image upload
image_uploader.php located in root > includes directory - the page that contains the upload form, made as a seperate file to be included into any page that may require photo uploads.
photo_system.php located in root > parsers directory - the page that handles image upload.
When I go to the create a post page, click the link to upload image(s) all works well, I can see the form, I can even select the files I would like to try and upload but when I click the upload button nothing seems to happen, when photo_system.php should load.
I've added a line of code to photo_system.php right at the top that should bring up an alert box once the page has loaded, in an attempt to debug my issues, but I never see this unless I go directly to image_uploader.php
This has lead me to believe that maybe my relative links to files were the issue so I've tried appending $_SERVER['DOCUMENT_ROOT'] to the start of the links to ensure it can alway locate them from the root directory, currently I have it set up with a hard coded $root variable and still I can't seem to get the system to work.
Is it likey that the file structure and the way the pages interact with each other is my problem? Could anyone take a look at my code for these three files and see if they can spot anything? I've been stumped by this for a week or so now so I think its time to ask for some outside help.
I have my 3 files saved in a txt format so the code can be viewed
http://www.vwrx-project.co.uk/temp_source_code/create_post.txt
http://www.vwrx-project.co.uk/temp_source_code/image_uploader.txt
http://www.vwrx-project.co.uk/temp_source_code/photo_system.txt
It turns out that in the end I was trying to nest two tables one inside the other so this was part of my issue.
The way that I had it set up I was including the photo upload system, image_uploader.php, as a table with an action requesting the photo_system.php file which worked when I went to the image_uploader.php page directly.
I also had in my main page where the images were being uploaded a form that had been set to onsubmit = false and this was canceling out the form action of the included file.
I only found this out when I decided to try and code it straight into one file else I'd still be stuck now.

PHP video upload

In my project, user will upload videos. So i have done simple code to check video file extension and moved to a particular folder. now problem is that only registered user can see that video. So i decided to check session while loading page which contain player to play video. but in view-source option , video file url available. so then can see that video using that url. now i need to check session before accessing that video folder. or is it possible to store video files in DB?
thanks in advance..
It is insane, you need to make a rewrite or something like this video.php?id=blablabla, a then check if user is logged in, if he is, play (readfile) video. What type of file is it?
$id = $_GET["id"];
if($session == true)
{
header("Content-type: video/flv");
readfile("my/secret/path/".$id);
}
everything that's shown for a user can always be copied and spread, but one way to prevent it from a more regular user would be using a php that reads an id/hash(basicaly a key of some sort) that is related to an video and then loads the video through file_read and outputs it.
store key and then the real url in a database then query the id to get the real url and then use a file reading function for example to stream it
the function could be fopen, fgets or something similiar

Categories