|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Uploading Files to Web Servers
By: FYICenter.com
Part:
1
2
3
(Continued from previous part...)
How To Move Uploaded Files To Permanent Directory?
PHP stores uploaded files in a temporary directory with temporary file names. You must move uploaded files
to a permanent directory, if you want to keep them permanently. PHP offers the move_uploaded_file() to help
you moving uploaded files. The example script, processing_uploaded_files.php, below shows a good example:
<?php
$file = '\fyicenter\images\fyicenter.logo';
print("<pre>\n");
move_uploaded_file($_FILES['fyicenter_logo']['tmp_name'], $file);
print("File uploaded: ".$file."\n");
print("</pre>\n");
?>
Note that you need to change the permanent directory, "\fyicenter\images\",
used in this script to something else on your Web server. If your Web server is provided by
a Web hosting company, you may need to ask them which directories you can use to store files.
If you copy both scripts, logo_upload.php and processing_uploaded_files.php, to your Web server,
you can try them to upload an image file to your Web server.
How To Detect File Uploading Errors?
If there was a problem for a file upload request specified by the <INPUT TYPE=FILE NAME=fieldName...> tag,
an error code will be available in $_FILES[$fieldName]['error']. Possible error code values are:
- UPLOAD_ERR_OK (0) - There is no error, the file uploaded with success.
- UPLOAD_ERR_INI_SIZE (1) - The uploaded file exceeds the upload_max_filesize directive in php.ini.
- UPLOAD_ERR_FORM_SIZE (2) - The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
- UPLOAD_ERR_PARTIAL (3) - The uploaded file was only partially uploaded.
- UPLOAD_ERR_NO_FILE (4) - No file was uploaded.
- UPLOAD_ERR_NO_TMP_DIR (5) - Missing a temporary folder.
Based on the error codes, you can have a better logic to process uploaded files more accurately,
as shown in the following script:
<?php
$file = '\fyicenter\images\fyicenter.logo';
$error = $_FILES['fyicenter_logo']['error'];
$tmp_name = $_FILES['fyicenter_logo']['tmp_name'];
print("<pre>\n");
if ($error==UPLOAD_ERR_OK) {
move_uploaded_file($tmp_name, $file);
print("File uploaded.\n");
} else if ($error==UPLOAD_ERR_NO_FILE) {
print("No files specified.\n");
} else {
print("Upload faield.\n");
}
print("</pre>\n");
?>
If you try this script with logo_upload.php and do not specify any files, you will get the "No files specified."
message.
Why Do You Need to Filter Out Empty Files?
When you are processing uploaded files, you need to check for empty files, because they could be resulted
from a bad upload process but the PHP engine could still give no error. For example, if a user typed a bad file
name in the upload field and submitted the form, the PHP engine will take it as an empty file without raising
any error. The script below shows you an improved logic to process uploaded files:
<?php
$file = '\fyicenter\images\fyicenter.logo';
$error = $_FILES['fyicenter_logo']['error'];
$tmp_name = $_FILES['fyicenter_logo']['tmp_name'];
print("\n");
if ($error==UPLOAD_ERR_OK) {
if ($_FILES['fyicenter_logo']['size'] > 0) {
move_uploaded_file($tmp_name, $file);
print("File uploaded.\n");
} else {
print("Loaded file is empty.\n");
}
} else if ($error==UPLOAD_ERR_NO_FILE) {
print("No files specified.\n");
} else {
print("Upload faield.\n");
}
print("\n");
?>
How To Create a Table To Store Files?
If you using MySQL database and want to store files in database,
you need to create BLOB columns, which can holds up to 65,535 characters.
Here is a sample script that creates a table with a BLOB column to be used to store uploaded files:
<?php
$con = mysql_connect("localhost", "", "");
mysql_select_db("fyi");
$sql = "CREATE TABLE fyi_files ("
. " id INTEGER NOT NULL AUTO_INCREMENT"
. ", name VARCHAR(80) NOT NULL"
. ", type VARCHAR(80) NOT NULL"
. ", size INTEGER NOT NULL"
. ", content BLOB"
. ", PRIMARY KEY (id)"
. ")";
mysql_query($sql, $con);
mysql_close($con);
?>
(Continued on next part...)
Part:
1
2
3
|