简体   繁体   中英

Uploading images with size limitations in PHP/MySQL

I'm trying to figure out how to upload imaged to a folder. I don't want to list it by listing all files in the directory.

What I want to do is have the user insert certain values to put into the MySQL, then take the file name when the file is finished uploading and insert it into a MySQL field "filename".

Thing is, I don't really know how to do file uploads in PHP, much less how to have it not add the information to the database until the file is fully uploaded. Could someone please help me out?

I also wanted to know how I could limit the max filesize to a certain size. I haven't been following technology for a while, so I wanted to know what the average size of an image would be of a photo take with an average digital camera now of days?

Lets make sure we have a form in a page. Lets call it form.php:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Then we create a file called uploader.php in the same folder as form.php.

// change the username and password to your database information
mysql_connect("localhost", "username", "password");
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
// Here we will insert into the database
mysql_query("INSERT INTO fileuploads (filename) VALUES ('".$target_path."')");

Now make sure that you have a table called fileuploads with the field "filename".

Don't forget to create a directory in the same folder as form.php and uploader.php called "uploads"

I hope this will work for you.

You can easily find out the PHP file upload details with example from this url http://php.net/manual/en/function.move-uploaded-file.php And to verify the size, you should use getimagesize() function in php. Check the example code here http://php.net/manual/en/function.getimagesize.php

Well you seem to have broken it down into the steps yourself so you seem to understand what to do.

The first thing you need is to upload the file in PHP. You can find a tutorial on it here . You will need a form in HTML and a PHP file which will handle it. This is also where you specify the file size.

Sample HTML

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Sample PHP

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

Then once you have uploaded the file you will need to store it in your database. You need to connect to your database ( tutorial here )

Then you will need to add the information to the table, the filename is in the var $_FILES['uploadedfile']['name'] so you would need some code along the lines of

mysql_query("INSERT INTO files (Date, Filename)
VALUES ('$date', '$_FILES['uploadedfile']['name']')");

More information on inserting into a database can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM