Move files in php with move_uploaded_file [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a question, now all my video files are in /videos now I want to move all my files in /videos/2014/05 or /videos/2014/06...in my database I have a field call date(when the video
was uploaded) now how to create this script to make folders /2014/05 and moved all videos there?
I tried an example but not received.
public function move()
{
$today_folders = date('Y') .'/'. date('m'). '/' ;
if ( !file_exists( $this->config->item("multimedia_path") . 'images/'. $today_folders) ){
$old_umask = umask(0);
mkdir( $this->config->item("multimedia_path") . 'videos/'. $today_folders, 0777, true );
umask($old_umask);
$this->load->database();
$articles=$this->db->query("SELECT *FROM videos ORDER BY date DESC");
$source = "videos/";
$destination = "videos/".$today_folders;
foreach ($articles as $file) {
if (in_array($file, array(".",".."))) continue;
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
foreach ($delete as $file) {
unlink($file);
}
}

public function move()
{
if ( !file_exists( $this->config->item("multimedia_path") . 'images/'. $today_folders) ){
$old_umask = umask(0);
$this->load->database();
// Assuming mp4_video is your file name
$videos=$this->db->query("SELECT mp4_video, date FROM videos ORDER BY date DESC");
$source = "videos/";
foreach ($videos as $video) {
// Load the destination for the video based on the date it was uploaded, assuming your DB wrapper converts date into a DateTime object, if not let me know it's type(eg int because it's a timestamp, or string because it's a mysql datetime, or whatever)
$destination = $this->config->item("multimedia_path") . 'videos' . $video['date']->format('/Y/m/d/');
// Again, assuming mp4_video is your filename
$file = $video['mp4_video'];
mkdir( $destination, 0777, true);
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
umask($old_umask);
foreach ($delete as $file) {
unlink($file);
}
}

Related

php - Get the last modified dir

A little stuck on this and hoping for some help. I'm trying to get the last modified dir from a path in a string. I know there is a function called "is_dir" and I've done some research but can't seem to get anything to work.
I don't have any code i'm sorry.
<?php
$path = '../../images/';
// echo out the last modified dir from inside the "images" folder
?>
For example: The path variable above has 5 sub folders inside the "images" dir currently right now. I want to echo out "sub5" - which is the last modified folder.
You can use scandir() instead of is_dir() function to do it.
Here is an example.
function GetFilesAndFolder($Directory) {
/*Which file want to be escaped, Just add to this array*/
$EscapedFiles = [
'.',
'..'
];
$FilesAndFolders = [];
/*Scan Files and Directory*/
$FilesAndDirectoryList = scandir($Directory);
foreach ($FilesAndDirectoryList as $SingleFile) {
if (in_array($SingleFile, $EscapedFiles)){
continue;
}
/*Store the Files with Modification Time to an Array*/
$FilesAndFolders[$SingleFile] = filemtime($Directory . '/' . $SingleFile);
}
/*Sort the result as your needs*/
arsort($FilesAndFolders);
$FilesAndFolders = array_keys($FilesAndFolders);
return ($FilesAndFolders) ? $FilesAndFolders : false;
}
$data = GetFilesAndFolder('../../images/');
var_dump($data);
From above example the last modified Files or Folders will show as Ascending order.
You can also separate your files and folder by checking is_dir() function and store the result in 2 different arrays like $FilesArray=[] and $FolderArray=[].
Details about filemtime() scandir() arsort()
Here's one way you can accomplish this:
<?php
// Get an array of all files in the current directory.
// Edit to use whatever location you need
$dir = scandir(__DIR__);
$newest_file = null;
$mdate = null;
// Loop over files in directory and if it is a subdirectory and
// its modified time is greater than $mdate, set that as the current
// file.
foreach ($dir as $file) {
// Skip current directory and parent directory
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir(__DIR__.'/'.$file)) {
if (filemtime(__DIR__.'/'.$file) > $mdate) {
$newest_file = __DIR__.'/'.$file;
$mdate = filemtime(__DIR__.'/'.$file);
}
}
}
echo $newest_file;
This will work too just like the other answers. Thanks everyone for the help!
<?php
// get the last created/modified directory
$path = "images/";
$latest_ctime = 0;
$latest_dir = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
if(is_dir($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_dir = $entry;
}
} //end loop
echo $latest_dir;
?>

how to unzip multiple zip files [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have several ziped files in a folder. I would like to unzip them to a specified folder. I have the following php code:
$path = "docs/" . $ID;
$files = scandir("temp" . '/' . $ID );
foreach ($files as $athely){
$zip = new ZipArchive;
$res = $zip->open($athely);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $athely";
}
}
It is not working. What am I doing wrong?
The problem was you are not using full path for opening the zip. Another thing to notice is if more than one zip file have folder with same name then one folder will overwrite the other.
<?php
$path = "docs/" . $ID;
$files = scandir("temp" . '/' . $ID );
foreach ($files as $athely){
if($athely=="." || $athely=="..") continue;
$target_path = "temp/".$ID."/".$athely;
$file = $athely;
$zip = new ZipArchive;
$res = $zip->open($target_path);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $athely";
}
}
?>

Delete all but one filetype from directory using glob php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does glob() have negation?
I want to delete all files from a directory (could be any number of file extentions) apart from the single index.html in there.
I'm using:
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
unlink($file);
}
But can't for the life of me how to say unlink, if not .html!
Thanks!
Try this here...
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
$pathPart = explode(".",$file);
$fileEx = $pathPart[count($pathPart)-1];
if($fileEx != "html" && $fileEx != "htm"){
unlink($file);
}
}
try
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
if(pathinfo($file, PATHINFO_EXTENSION) != 'html') {
unlink($file);
}
}
if you want to delete other html files also (apart from "index.html"):
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
if(pathinfo($file, PATHINFO_BASENAME) != 'index.html') {
unlink($file);
}
}
The php function glob has no negation, however PHP can give you the difference between two globs via array_diff:
$all = glob("*.*");
$not = glob("php_errors.log");
var_dump(
$all,
$not,
array_diff($all, $not)
);
See the demo: http://codepad.org/RBFwPUWm
If you do not want to use arrays, I highly suggest to take a look into PHPs directory iterators.

Retrieve the list of alla jpg file of a directory [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Finding a file with a specific name with any extension
i've this code for put into an array all the files contained into a directory
$directory = "./";
$ourDirList = scandir($directory);
$arraylistafiles=array();
foreach($ourDirList as $ourItem)
{
if (is_file($ourDir . $ourItem))
{$arraylistafiles[]=$ourItem;}
}
but if i want to put only the file that have ".jpg" extension, what i can do?
Using PHP's glob() you can avoid the is_file() call because glob will only return files that actually exist in the directory. There is no need to create a UDF (user defined function) in your case.
$dir = './';
foreach(glob($dir.'*.jpg') as $file) {
print $file . "\n";
}
UPDATE
From your comment it's clear that you don't understand how glob() works. You can achieve what you're trying to do like this:
$arraylistafiles = glob($dir.'*.jpg');
if(is_file($ourDir.$ourItem) && substr($ourItem,-4) == '.jpg') {
//
}
You can use bellow function.
public function getFileList($dirpath,$list_ignore,$list_allowed_ext)
{
$filelist = array();
if ($handle = opendir(dirname ($dirpath)))
{
while (false !== ($file = readdir($handle)))
{
if (!is_dir($file) && !in_array($file,$list_ignore) && in_array(strtolower(end(explode('.',$file))),$list_allowed_ext))
{
$filelist[] = $file;
}
}
closedir($handle);
}
return $filelist;
}
Implementation will be looks like..
$fileTypes = array ("jpg");
$list_ignore = array ('.','..');
$fileList = getFileList("./",$list_ignore,$fileTypes);
Cheers!
First you have to check for the file extension for the file in foreach array. . If the extension is jpg then add that item into the new array. . . Hope you understand. .

check for files with PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get the Files inside a directory
Is there a function that can be used to get the contents of a directory (a photo gallery directory for example) ?
I'm trying to save time on a project by automating a photo gallery based on which files are available.
Thanks
Shane
You can either use the DirectoryIterator:
$dir = new DirectoryIterator('path/to/images');
foreach ($dir as $fileinfo) {
echo $fileinfo->getFilename() . "\n";
}
or alternatively glob():
$filenames = glob('path/to/images/*.jpg');
foreach ($filenames as $filename) {
echo $filename ."\n";
}
glob()
scandir()
I use a while loop to grab a list of files, omit the 2nd if statement if you want to grab a all files.
if ($handle = opendir('/photos/')) {
while(false !== ($sFile = readdir($handle))) {
if (strrpos($sFile, ".jpg") === strlen($sFile)-strlen(".jpg")) {
$fileList[] = $sfile;
}
}
}

Categories