So, I want to create a system where user uploads in a zip file the files of a 3d model and the model can be shown, stored, etc
So, I got the file, I place it into a folder, permanently, And unzip it into another temp folder, just to see if it is a 3d model.
I tried like this:
$target_dir = "upload/";
$targetfilename = rand().$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $target_dir.$targetfilename);
//unzip the file into temp folder
$tmp_dir = $target_dir.rand();
mkdir($tmp_dir);
chmod($tmp_dir, 0777);
//chmod($targetfilename, 0777); //this not working, maybe isn't the right way
$zip = new ZipArchive;
$res = $zip->open($targetfilename);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($tmp_dir);
$zip->close();
echo 'SUCCESS';
} else {
echo 'ERROR';
}
I do not get any errors, but the zip can't be unzipped. Any idea? How can I resolve this?
Isn't $targetfilename is in $target_dir folder?
If so, changing
$res = $zip->open($targetfilename); to
$res = $zip->open($target_dir.$targetfilename); might solve your problem.
Related
I have a image named xyz. Besides this file named xyz has unknown extension viz jpg,jpeg, png, gif etc. I want to copy this file from one folder named advertisers/images to other folder publishers/images in my website cpanl. How to do this with php. Thanks in advance.
You can use copy function:
$srcfile = 'source_path/xyz.jpg';
$dstfile = 'destination_path/xyz.jpg';
copy($srcfile, $dstfile);
You should write your code same as below:
<?php
$imagePath = "../Images/somepic.jpg";
$newPath = "../Uploads/";
$ext = '.jpg';
$newName = $newPath."a".$ext;
$copied = copy($imagePath , $newName);
if ((!$copied))
{
echo "Error : Not Copied";
}
else
{
echo "Copied Successful";
}
?>
Use copy() function
copy('advertisers/images/xyz.png','publishers/
images/xyz.png');
Change the file extension, whatever it is.
If you don't know the extension, go with the wildcard. It will give you the array of all the files matching with the wildcard.
$files = glob('advertisers/images/xyz.*');
foreach ($files as $file) {
copy($file,'publishers/images/'.$file);
}
Need to insert uploaded file into respected folder
Here i am creating folder based on unique id.
I am not able to insert file into the folder.
when file is uploaded both file and folder are stored separately.
if ($file_check_error == 0){
if(move_uploaded_file($file['tmp_name'],$upload_directory.$path)){
echo"inside 2"."<br>";
echo"Your File Successfully Uploaded";
}
$path_user = '/home/devestctrl/public_html/wp-content/plugins/est_collaboration/Files/'.$send_id;
if (!file_exists($path_user)) {
if (mkdir( $path_user,0766,false )) {
$path_move = $path_user."/".$path;
echo $path_user;
rename($path,$path_move);
echo "Success+++++++++++";
}
else{
echo $path;
echo "Failure+++++++++++";
}
}
}
$send id is unique id.
Please let me know where i have gone wrong?
You are messing up your logic. First you move your uploaded file
if (move_uploaded_file($file['tmp_name'], $upload_directory.$path)) {
and only than you try to create new directory
if (mkdir($path_user,0766,false )) {
and only if current user never uploaded anything you rename file moving it to other dir
rename($path,$path_move);
Correct logic:
Format uploaded file path $path = $upload_directory.DIRECTORY_SEPARATOR.$send_id
Check if dir exists file_exists($path)
If not exists, create it mkdir($path, 0766, false)
Upload file move_uploaded_file($file['tmp_name'], $path)
E.g.:
$path = $upload_directory.DIRECTORY_SEPARATOR.$send_id;
if (!file_exists($path)) {
mkdir($path, 0766, false);
}
move_uploaded_file($file['tmp_name'], $path);
I have the following code, which as you can see i use to create a new directory then unzip a file.
<?php
function unzip_to_s3() {
// Set temp path
$temp_path = 'wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/tmp/';
// Get filename from Zip file
$zip_file = 'archive.zip';
// Create full Zip file path
$zip_file_path = $temp_path.$zip_file;
// Generate unique name for temp sub_folder for unzipped files
$temp_unzip_folder = uniqid('temp_TMS_', true);
// Create full temp sub_folder path
$temp_unzip_path = $temp_path.$temp_unzip_folder;
// Make the new temp sub_folder for unzipped files
if (!mkdir($temp_unzip_path, '0755', true)) {
die('Error: Could not create path: '.$temp_unzip_path);
}
// Unzip files to temp unzip folder, ignoring anything that is not a .mp3 extension
$zip = new ZipArchive();
$filename = $zip_file_path;
if ($zip->open($filename)!==TRUE) {
exit("cannot open <$filename>\n");
}
for ($i=0; $i<$zip->numFiles;$i++) {
$info = $zip->statIndex($i);
$file = pathinfo($info['name']);
if(strtolower($file['extension']) == "mp3") {
file_put_contents(basename($info['name']), $zip->getFromIndex($i));
} else {
$zip->deleteIndex($i);
}
}
$zip->close();
}
unzip_to_s3();
?>
The unzip code was courtesy of #TotalWipeOut from one of my other posts. It currently unzips just mp3 files to my base directory, but i want to put them in my newly created folder.
I'm very new to PHP so have been trying my best with this, but i can't figure out how to change the file_put_contents(basename($info['name']), $zip->getFromIndex($i)); line to get it to put the files in my new folder?
As Marc B. mentioned you need to include the path to the directory you are putting the file in.
using your code:
file_put_contents($temp_unzip_path."/".basename($info['name']), $zip->getFromIndex($i));
I would also suggest reading a little more about the basics of PHP.
Hello I am usign the below code to zip a package on upload:
$nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './CopyrightFiles/';
$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";
It seems to work ok.
But there are two issues.
First issue it saves the file also with the original extension, something like : image.JPG.zip
Also when I then move the zip package to my local computer (Mac) and I open the ZIP inside I can find only a tmp folder with a binary file inside and NOT the image or the file that should be there!
What the hell is going on?
Please advise
Thank you
That's because your "binary" file is just the temporary name that PHP used to temporarily stored the uploaded file as. That's why it's "tmp_name" in the $_FILES array.
Try this:
$zip->addFile($tmpName, $nameFile);
The second parameter lets you specify what the file's name should be within the zip.
To prevent 'file.jpg.zip', try:
$fileconpress = $download_folder . pathinfo($_FILES['file']['name'], PATHINFO_FILENAME) . ".zip";
Issue #1:
You need to manually remove the extension:
$filename = explode('.', $_FILES['file']['name']);
$filename = $filename[0];
Issue #2:
$tmpName does not contain the filename of the file. You need to pass the $localname parameter in addFile(). See http://php.net/manual/en/function.ziparchive-addfile.php.
I'm using the Uploadify jQuery plugin for PHP to upload a file. One thing I am stuck on is that I need to be able to rename the file being uploaded so that I can post that information to my script that inserts data into the mysql database. Can anyone please advise on how to do this?
Thanks,
Jake
Firstly rename the filename as you want in your uploadify.php
Then you just need to return $targetPath from your uploadify.php file, like this --
echo $targetPath
no need to use echo '1'
And now you have to get the renamed file name. You can get this in onUploadSuccess function.
onUploadSuccess() takes three parameters (file, data, response),
where file gives you the actual name of file you browsed through your computer, and data gives you the renamed file name which you generated through your uploadify.php code as per your requirement.
So you can try the below code --
'onUploadSuccess' : function(file, data, response) {
alert('Renamed file name is - ' + data);
}
I hope this will help out. One of my friend told me this, and I got my work done then :)
you can do like this :-
$targetFolder = FCPATH.'/resources/images/users/temp/'; // Relative to the root
if (!empty($image))
{
$time=strtotime("now");
$image['Filedata']['name']=$time.'.jpg';
$tempFile = $image['Filedata']['tmp_name'];
$targetPath = $targetFolder.$image['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($image['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes))
{
move_uploaded_file($tempFile,$targetPath);
echo '1';
}
else
{
echo 'Invalid file type.';
}
}
If you use uploadify.php just go right before the function move_uploaded_files and change the target name.
Anyways you do it this should work. Post the code you have if you want a more detailed answer.
This will put file in new folder with same file name as source file
$source = $_FILES['Filedata']['tmp_name'];
$filename = $_FILES['Filedata']['name'];
$newPath = $folder.'/'.$filename;
rename($source, $newPath);
/*----------------------*/
to have a new filename
function getExtension($path)
{
$result = substr(strtolower(strrchr($path, '.')), 1);
$result = preg_replace('/^([a-zA-Z]+)[^a-zA-Z].*/', '$1', $result);
if ($result === 'jpeg' || empty($result) === true) {
$result = 'jpg';
}
return $result;
}
$source = $_FILES['Filedata']['tmp_name'];
$filename = $_FILES['Filedata']['name'];
$newfileName=$folder."/"."abc".getExtension($filename);
rename($source, $newfileName);