View uploaded file in php

I'm having some issues with uploading files from HTML to a PHP based web server. The following is my HTML code. No issues here I believe.


 

  

PHP code:

 0 
    && $_FILES['file']['size'] <= MAX_FILE_SIZE) {
      move_uploaded_file($_FILES, $uploadedfile);
      echo ('');
}
?>

I can't figure out how to make it work in PHP. I've been trying several different ways and the above code is just my latest desperate attempt at figuring it out by trying to store the value into a new variable. What I want to do is the following:

  • Restrict the type and size of files that can be uploaded.
  • Is the file a picture? Display the picture.
  • Is it a plaintext file? Display the contents of the file.
  • Is it an unpermitted file? Display the name, type and size of the file.

Any kind of hint or help in any way would be greatly appreciated. Thanks!

PS. It's not going live, so any security issues are irrelevant at the moment.

Hi! In this tutorial let me show you about upload, view and download file in php and mysql. The file uploading process is similar to what we have discussed here, but this php script not only uploads file to the server but also stores the file path and its created date in mysql database. Apart from uploading file, it also gives you the option to view file on browser and download it from server.

With PHP you can practically upload any type of files and the file uploading script I have shared below will work for all file types like PDF, Document, Images, MP3, Videos, Zip archives etc. Just include those file extensions in the filtering process ($allowed array) and you will be able to upload them.

How to Upload, View & Download File in PHP & MySQL?

Let's move on to the coding part. First you should create mysql database to store file details.

Create MySQL Database:

CREATE DATABASE `demo` ;
Use `demo`;
CREATE TABLE IF NOT EXISTS `tbl_files` (
  `id` int(9) NOT NULL AUTO_INCREMENT,
  `filename` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Next is the database connectivity script that establishes connection to mysql database from php.

dbconnect.php

Then create index.php - this is the main file containing user interface. It has an upload form and a html table to display the list of uploaded files from database along with 'View' & 'Download' links for them.

index.php





    Upload View & Download file in PHP and MySQL | Demo
    
    



Select File to Upload:
# File Name View Download
View Download

Note: This demo uses twitter bootstrap for css stylesheet.

Running index.php will generate a page with upload form and table with files details similar to this. Users can either click on 'View' link to view the files on browser or on 'Download' to download the files from server.

View uploaded file in php

Finally there is 'uploads.php' file which will be executed when the form is submitted to upload the selected file. Here is where we actually upload the file to the server from the client machine and save its name and uploaded date into the database.

uploads.php

 0)
            {
                $row = mysqli_fetch_array($result);
                $filename = ($row['id']+1) . '-' . $filename;
            }
            else
                $filename = '1' . '-' . $filename;

            //set target directory
            $path = 'uploads/';
                
            $created = @date('Y-m-d H:i:s');
            move_uploaded_file($_FILES['file1']['tmp_name'],($path . $filename));
            
            // insert file details into database
            $sql = "INSERT INTO tbl_files(filename, created) VALUES('$filename', '$created')";
            mysqli_query($con, $sql);
            header("Location: index.php?st=success");
        }
        else
        {
            header("Location: index.php?st=error");
        }
    }
    else
        header("Location: index.php");
}
?>

This script upload file from local machine to server and stores its details into database and redirects to index.php. If everything goes right you will be able to see success message on completion.

View uploaded file in php

If there's any error you will be notified about it.

View uploaded file in php

Also Read:

  • How to Download File from URL with PHP & CURL
  • PHP User Login and Signup System using MySQL

So we have seen about file upload and to view and download them using php and mysql database. If you want you can set the file size limit or restrict users to upload only pdf or images etc.

How can I view uploaded files in PHP?

In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded. The file refers to the name which is defined in the “index. html” form in the input of the file.

How can I view uploaded image in PHP?

"upload/" . $_FILES["file"]["name"]. "
"; $image=$_FILES["file"]["name"]; /* Displaying Image*/ $img="upload/".
$image; echo '

How can I recover my uploaded file in PHP?

4 Answers. Sorted by: ... .
Upload form. What does your form tag look like? ... .
Sanitisation. $company = mysql_real_escape_string($_POST['company']); $location = mysql_real_escape_string($_POST['location']); $pic = mysql_real_escape_string($_FILES['userfile']['name']); ... .
SQL Query. ... .
HTML Output..

Where does PHP store uploaded files?

php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php. ini.