[PHP] upload file and zip with password

Background (needs):
1. upload a file without checking the type (whatever the type)
2. move the file into a specific folder
3. zip the file with password

Thanks to the function move_uploaded_file, it is very simple to move file to wherever you want, of course, you must have the privilege to do that.

As it is quite simple, I put the source code directely here :

The HTML part :

<!DOCTYPE html>
<form action="upload.php" method="POST" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">

The PHP part :

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

// Check if file already exists
if (file_exists($target_file)) {
	echo "The file uploaded already exists.";
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
	echo "Sorry, your file is too large.";
// Put the file uploaded into a specific folder
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
	echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

	exec("chmod 777 ".$target_file);	// Give the right to modify the file
	exec("zip -P your_password $target_dir/$target_file.zip ".$target_file);	// Archive the file with a password
} else {
	echo "Sorry, there was an error uploading your file.";

If you got an error, try to check especially the privilege.

To find more examples and demos, please find w3schools and the offical site of php.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s