I am currently building a backend to a site using the codeigniter framework, I have hit a bit of a problem, I needing a way to allow the user to upload a zipped folder of images, on completing the form, zipped folder must be unzipped, and the files need to be moved to a folder else where on the server, have thumbnail version of each image created and have there file name add to the DB, and also if the images are for a content type that does not already exist then I need to make a directory with that content type.
I know there is an upload class in Codeigniter, but I am not sure that, that has the capabilities do what I need, I could really do with some advice please?
Thanks
As Jan pointed out, this is a broad question (like 3 or 4 questions). I'm not up to date with the CodeIgniter framework but to Unzip the files you can do something like this:
function Unzip($source, $destination)
{
if (extension_loaded('zip') === true)
{
if (file_exists($source) === true)
{
$zip = new ZipArchive();
if ($zip->open($source) === true)
{
$zip->extractTo($destination);
}
return $zip->close();
}
}
return false;
}
Unzip('/path/to/uploaded.zip', '/path/to/extract/');
You wont be able to do any of the image or file checking using the Upload class. The upload class will let you accept the file and check it is a ZIP but that's as far as it will go.
From there, unzip the file and just do some simple PHP on the files to check they are the right type and make your folders etc. I would put this logic in a new library to keep it separated correctly.
Related
I have some encrypted responses that I convert to a Zip file in my Laravel application. The function below downloads the API response, saves it as a Zip file, and then extracts it while I read the folder's contents. In my local environment, it works well. However, the Zip file is not getting saved to the storage folder on the live server. No error is being shown, only an empty JSON response. Please, what could be the cause?
public function downloadZipAndExtract($publication_id, $client_id)
{
/* We need to make the API call first */
$url = $this->lp_store."clients/$client_id/publications/$publication_id/file";
$file = makeSecureAPICall($url, 'raw');
// Get file path. If file already exist, just return
$path = public_path('storage/'.$publication_id);
if (!File::isDirectory($path)) {
Storage::put($publication_id.'.zip', $file);
// Zip the content
$localArchivePath = storage_path('app/'.$publication_id.'.zip');
$zip = new ZipArchive();
if (!$zip->open($localArchivePath)) {
abort(500, 'Problems experienced while reading file.');
}
// make directory with the publication_id
// then extract everything to the directory
Storage::makeDirectory($publication_id);
$zip->extractTo(storage_path('app/public/'.$publication_id));
// Delete the zip file after extracting
Storage::delete($publication_id.'.zip');
}
return;
}
First thing I'd check is if the storage file is created and if it isn't created, create it. Then I'd look at your file permissions and make sure that the the groups and users permissions are correct and that you aren't persisting file permissions on creation. I've had many instances where the process that's creating files(or trying) is not in the proper group and there is a sticky permission on the file structure.
I'm developing a web app where users could upload .zip file and storing it in the Google Drive. The upload part was easy but I have no clue how to unzip the file uploaded.
I found this but it's in Javascript and am still trying to find out how to unzip the file using PHP. Anyone knows how to accomplish this?
I guess you can use the ZipArchive class, i.e.:
$zip = new ZipArchive;
$res = $zip->open('myfile.zip');
if ($res === TRUE) {
$zip->extractTo('/extract/path/');
$zip->close();
echo 'ok!';
} else {
echo 'error!';
}
Didn't find a solution, I don't think unzipping files on Drive using PHP is possible by now. So instead of uploading .zip, I extract the zip to temp folder then loop to read the files. Here are the steps I do :
loop items in folderId as item
if item == folder
create folder on Drive
fetch folder id as folderId
loop(folderId)
else if item == file
file.parent = folderId
upload file
endif
endloop
I have gone through the various post regarding zip and to read zip that is extracting zip downloading it and then access it. But i want to check the format and name convention for the files enclosed in Zip file that is going to be uploaded. I am using drag drop feature here and planning that when user drop the zip file it should extract the file but don't store it any where and check that all the image uploaded in the zip are of same format and name convention are followed like img1.jpg, img2.jpg.
Any help is deeply appreciated. I am using codeignitor framework and is it possible to extract and manipulate zip using backbone or java script.
I don't think you can check the contents of the zip before uploading.. Here's a code , you can very well use to check if the content inside is legit. [In this example... Assuming img1.zip contains an image file img1.jpg]
Maybe a start
<?php
$zip = new ZipArchive;
$res = $zip->open('img1.zip');
if ($res) {
$legitImage=explode('.',$zip->statIndex(0)['name']);
if($legitImage[1]=='jpg')
{
echo "It's an image";
//do your operations
$zip->close();
}
else
{
unlink('img1.zip');//Delete the zip file since it does not contain image
}
}
?>
I am using codeigniter to create the admin panel of my website. I am not using it for the front end because I have lots of static pages in the front end and only a few things need to be dynamic so I will be doing it with core PHP queries.
My link for unlinking photo is , images is the controller and unlinkPhoto is the function and 32 is the image ID.
localhost/admin/index.php/images/unlinkPhoto/32
EDIT
however my image is located at localhost/uploads/testimage.jpg.
How can I point to that folder to unlink the image in codeigniter.
You really need to make sure you're enforcing decent security protocols, otherwise anyone can fake the GET request and delete the entirety of your uploaded files. Here is a basic solution:
public function unlinkPhoto($photoId)
{
// Have they specified a valid integer?
if ((int) $photoId > 0) {
foreach (glob("uploads/*") as $file) {
// Make sure the filename corresponds to the ID
// Caters for all file types (not just JPGs)
$info = pathinfo($file);
if ($info['filename'] == $photoId) {
unlink($file);
}
}
}
}
If you're using PHP 5.4, you might be able to reduce this code down even more:
if (pathinfo($file)['filename'] == $photoId) {
unlink($file);
}
Because they've implemented array dereferencing (finally). Although I haven't tested this particular piece of code. This is just a geeky addendum.
I have searched far and wide on this one, but haven't really found a solution.
Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.
Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.
So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.
I am thinking I will need to use PHP to
1) upload the file
2) delete the original song.mp3
3) rename the new file upload to song.mp3
Does that seem right? Or is there a simpler way of doing this? Thanks in advance!
EDIT: I impimented UPLOADIFY and am able to use
'onAllComplete' : function(event,data) {
alert(data.filesUploaded + ' files uploaded successfully!');
}
I am just not sure how to point THAT to a PHP file....
'onAllComplete' : function() {
'aphpfile.php'
}
???? lol
a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.
then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory
<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name = $_FILES['myupload']['name'];
$type = $_FILES['myupload']['type'];
$size = $_FILES['myupload']['size'];
$tmp = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
// echo "The file $filename exists";
// This will overwrite even if the file exists
move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way
?>
try this piece of code for upload and replace file
if(file_exists($newfilename)){
unlink($newfilename);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename);