Currently the zip is downloading and the images within the zip are off the names that are stored on the server i need to rename them
I have 3 images on the server i need to download the images but before adding files to zip i need to rename them
public function downimages(){
$this->_check(); // session checker
$this->load->helper('download');
$this->load->library('zip');
$idm=$this->uri->segment(3);
$ab=$this->adminmod->downimg($idm);
$down=explode('-/,-', $ab[0]->director); //gets the directory stores in array
$fname=explode('-/,-', $ab[0]->file_oname); //gets the file names stores in array
foreach ($down as $key =>$value){
$name = $fname[$key]; // this is where the name is stored need to use this to rename
$data = "./"($value);
$this->zip->read_file($data); //adds the images to zip archive
}
$this->zip->download($idm.'.zip'); //downloads the zip fles
// redirect($_SERVER['HTTP_REFERER']);
}
My server image names are like xfasf.jpg 2) sadweq.jpg 3)wqweq.jpg
my actual file names are image1.jpg 2)blue.jpg 3)red.jpg whcih comes from $name
How do i go about this?
Solution Was Quite Easy
Solved IT at the $this->zip->read_file($data);
I just put the $name like this
$this->zip->read_file($data,$name);
now the files get renamed before they get zipped and then downloaded
Solved IT at the $this->zip->read_file($data); i jsut put the $name like this $this->zip->read_file($data,$name); now the files get renamed before they get downloaded
Related
I got a csv file, that looks like the following image :
Now there are pdf files in a dir(resources/pdfFiles) that match the Number columns (1,2,3 etc) of that csv file. Now what I am trying to do is, when a button is triggered, all those pdf files will be moved to another folder. But I have a twist here,The Name column of that csv file are the names of subfolders where the associate numbered files will be stored. For example,pdf files from 1-8 matches with Akaar IT Ltd, therefore these files (1-8) will be moved to the folder called Akaar It Ltd. If there is no such folder exists, then it will be generated automatically.
My code so far are :
$path = resource_path('pdfFiles');
$files = \File::files($path);
foreach($files as $key => $file) {
$filename = basename($files[$key]);
$destinationFilePath = public_path('upload/'.$filename );
rename($files[$key], $destinationFilePath);
}
dd("probably done");
What it's doing is moving all files from one folder to another.
Is it possible to use php to rename files in directory to match the output of a js file created. I have thousands of images downloaded from an API i was using , but switched my app to a new API and had to match up the old imageID with the new imageID. I'd rather batch rename the files instead of having the js file loaded to get the new id names. I have little experience in php and all the tutorials were simple file rename and nothing more complex like this.
img.js
var img_ar = {
"pid_12685":"2578377",
"pid_12757":"2980444",
"pid_12916":"3056906"
}
Current image file names /images
2578377.png
2980444.png
3056906.png
Desired file names after running php script
pid_12685.png (old file name 2578377.png)
pid_12757.png (old file name 2980444.png)
pid_12916.png (old file name 3056906.png)
you can make a php script (not necessary in your web server) and then call it by php your_script.php
in that script you can use scandir to get an array of all the images in your images directory
then you have to loop through that array and use the rename function to rename images
since i don't see a relation between old name number and the new name number
you can create an array
$names = [
'2578377.png' => 'pid_12685.png'
'2980444.png' => 'pid_12757.png'
'3056906.png' => 'pid_12916.png'
];
then, in your loop do rename($imageName, $names[$imageName]);
you can use this script:
either put the script in your images directory, or change the $path variable so the script will use the right directory
change $names to include all your images
<?php
$path = '.';
$files = array_diff(scandir($path), ['.', '..']);
$names = [
'2578377.png' => 'pid_12685.png',
'2980444.png' => 'pid_12757.png',
'3056906.png' => 'pid_12916.png'
];
foreach($files as $file){
if(is_file($file) && isset($names[$file])){
rename($file, $names[$file]);
}
}
call the script by php script_name.php
My purpose is uploading a remote file create from my PC to specific folder, but I don't know whats wrong with my code below. It uploads the file with the name and the .jpg extension, but it is not moving the file to the specified folder.
if(isset($_POST["image"])){
define("SITE_NAME","project_name/"); //constant for project name
define("SITE_PATH",$_SERVER['DOCUMENT_ROOT']."/".SITE_NAME); //constant for project base directory
define("IMAGES_URL",SITE_URL."images/"); //constant for image directory
$upload_base_dir=IMAGES_URL;
$upload_time_dir=date('Y')."/".date('m')."/".date('d')."/"; // setup directory name
$upload_dir = $upload_base_dir.$upload_time_dir;
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true); //create directory if not exist
}
$input = $_POST["image"];
$file = fopen(time()."image.jpg", 'wb');
fwrite($file, $input);
//$image_name=basename($_FILES['image']['name']);
$image=time().'_'.$image_name;
move_uploaded_file($file,$upload_dir.$image);
fclose($file);
}
Any suggestions? Thank you in advance.
move_uploaded_file($file,$upload_dir.$image) will only work for items within temp, that are accessable via $_FILES superglobal. If you are sending your file as a strieam within post, that wont work.
1) If file is a form upload make sure form is a multipart and access your file via $_FILES superglobal
move_uploaded_file($_FILES['userfile']['tmp_name'], $yourDirectory.$yourFilename);
2) If you post the file as a stream via post (keep in mind this will only work for small files as large ones will exceed request limit). Save the file directly to it's destiantion using fopen or to move it after you created it use rename() - http://php.net/manual/en/function.rename.php
rename($currentFilePath, $newFilePath)
P.S. sending files as post streams is a very very bad idea.
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
}
}
?>
What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)