DiegoP.
DiegoP.

Reputation: 45737

PHP file upload and zip on upload

Does anyone know of a good way to upload a file (.mp3 or .wav or .txt or .whatever) and zip this file before being moved to the directory?

So the user uploads a .mp3 file, before this file is moved to the specific directory it will be zipped and then moved.

Please advise.

Thank you

Upvotes: 1

Views: 12703

Answers (3)

Ali Lavasani
Ali Lavasani

Reputation: 1

I think it's the best way to upload each type of file in zip exe espesialy when you don't know that your clients or users upload what kind of file. for this way you should first set your direction in server where you want upload the file and then use:

<?php
$target_dir = '/www/yoursite.com/public_html/sth';
$ext = explode(".", $_FILES["filename1"]["name"]);
$ext = end($ext);
$newfilename = 'resume'. '.' .$ext;
$zip = new ZipArchive();
$zip->open('/www/yoursite.com/public_html/sth .'/'.'resume'.'.zip', 
ZipArchive::CREATE);
$zip->addFile($_FILES['filename1']['tmp_name'],$newfilename);
$zip->close();
?>

just pay attention to my guide as your friend

when you want to use this code you should know these points:

  1. $target_dir-->your server address where you want upload your file
  2. in this part of code i want to get the file extension until when i zip it keep the type of file

    $ext = explode(".", $_FILES["filename1"]["name"]);
    $ext = end($ext);
    $newfilename = 'resume'. '.' .$ext;
    
  3. here i named my zip file in my server

    $zip->open('/www/yoursite.com/public_html/sth .'/'.'resume'.'.zip',
    
  4. and in the end of code we get the upoloade file that i named here as "filename1(this is the name of your input in html)"

    $zip->addFile($_FILES['filename1']['tmp_name'],$newfilename);
    

Upvotes: 0

ben
ben

Reputation: 1936

Take a look at the Zip extension:

http://www.php.net/manual/en/zip.examples.php

I looked at the code you linked to (it would have been good if you included it in the question) and made a few changes:

$nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './files/';

$zip = new ZipArchive();
$fileconpress = $download_folder.$nameFile.".zip";

$conpress = $zip->open($fileconpress, ZIPARCHIVE::CREATE);
if ($conpress === true)
{
    $zip->addFile($tmpName);
    $zip->close();
    echo $fileconpress."<br/>";
    echo "yess !! Success!!!! ";
}
else echo " Oh No! Error";

The important part and likely what's causing your error is $download_folder. You need to define the path of where you want to save the file.

I also removed the fread(), you can just load the file straight into the zip object with addFile()

Upvotes: 5

Declan Cook
Declan Cook

Reputation: 6126

You can use PHP's ability to run shell commands and use them to make 3rd party software/your OS to zip your files for you. see - http://php.net/manual/en/function.shell-exec.php

Or there is Zip extensions for php see - http://php.net/manual/en/book.zip.php

Upvotes: 0

Related Questions