Friends,
I have a problem............
Help me please........
Am getting the image url from my client, i want to store those images in my local folder.
if those images are in less, i will save them manually
But they are greater than 5000 images.........
Please give some code to down load all the images with PHP
you could try file_get_contents for this. just loop over the array of files and use file_get_contents('url'); to retrieve the files into a string and then file_put_contents('new file name'); to write the files again.
You may download file using file_get_contents() PHP function, and then write it on your local computer, for example, with fwrite() function.
The only opened question is, where to get list of files supposed to be downloaded - you did not specify it in your question.
Code draft:
$filesList = // obtain URLs list somehow
$targetDir = // specify target dir
foreach ($filesList: $fileUrl) {
$urlParts = explode("/", $fileUrl);
$name = $urlParts[count($urlParts - 1)];
$contents = file_get_contents($fileUrl);
$handle = fopen($targetDir.$filename, 'a');
fwrite($handle, $contents);
fclose($handle);
}
I'm not sure that this is what you want. Given a folder's (where PHP has the authority to get the folder's contents) URL and a URL you want to write to, this will copy all of the files:
function copyFilesLocally( $source, $target_folder, $index = 5000 )
{
copyFiles( glob( $source ), $target_folder, $index );
}
function copyFiles( array $files, $target_folder, $index )
{
if( count( $files ) > $index )
{
foreach( $files as $file )
{
copy( $file, $target_folder . filename( $file ) );
}
}
}
If you're looking to a remote server, try this:
function copyRemoteFiles( $directory, $target_folder, $exclutionFunction, $index = 5000)
{
$dom = new DOMDocument();
$dom->loadHTML( file_get_contents( $directory ) );
// This is a list of all links which is what is served up by Apache
// when listing a directory without an index.
$list = $dom->getElementsByTagName( "a" );
$images = array();
foreach( $list as $item )
{
$curr = $item->attributes->getNamedItem( "href" )->nodeValue;
if( $exclutionFunction( $curr ) )
$images[] = "$directory/$curr";
}
copyFiles( $images, $target_folder, $index );
}
function exclude_non_dots( $curr )
{
return strpos( $curr, "." ) != FALSE;
}
copyRemoteFiles( "http://example.com", "/var/www/images", "exclude_non_dots" );
Related
I'm trying to encrypt .php file using phpBolt. when only with PHP code then it's working, But if it's a mix of HTML then it is not working.
Encryption code:
<?php
/**
* src : source folder
* encrypted : Output folder
*/
$src = 'src';
$php_blot_key = "kyc7fh";
/**
* No need to edit following code
*/
$excludes = array('vendor');
foreach($excludes as $key => $file){
$excludes[ $key ] = $src.'/'.$file;
}
// $rec = new RecursiveIteratorIterator(new RecursiveDirectoryIterator( $src ));
$rec = new DirectoryIterator($src);
$require_funcs = array('include_once', 'include', 'require', 'require_once');
foreach ($rec as $file) {
if ($file->isDir()) {
$newDir = str_replace( 'src', 'encrypted', $file->getPath() );
if( !is_dir( $newDir ) ) mkdir( $newDir );
continue;
};
$filePath = $file->getPathname();
if( pathinfo($filePath, PATHINFO_EXTENSION) != 'php' ||
in_array( $filePath, $excludes ) ) {
$newFile = str_replace('src', 'encrypted', $filePath );
copy( $filePath, $newFile );
continue;
}
$contents = file_get_contents( $filePath );
$preppand = '<?php define("PHP_BOLT_KEY", "kyc7fh"); bolt_decrypt( __FILE__ , PHP_BOLT_KEY); return 0;
##!!!##';
$re = '/\<\?php/m';
preg_match($re, $contents, $matches );
if(!empty($matches[0]) ){
$contents = preg_replace( $re, '', $contents );
##!!!##';
}
/*$cipher = bolt_encrypt( "?> ".$contents, $php_blot_key );*/
$cipher = bolt_encrypt( $contents, $php_blot_key );
$newFile = str_replace('src', 'encrypted', $filePath );
$fp = fopen( $newFile, 'w');
fwrite($fp, $preppand.$cipher);
fclose($fp);
unset( $cipher );
unset( $contents );
}
$out_str = substr_replace($src, '', 0, 4);
$file_location = __DIR__."/encrypted/".$out_str;
echo "Successfully Encrypted... Please check in <b>" .$file_location."</a></b> folder.";
encryption not working in this .php file :
<html>
<body>
<h1>
<?php
echo "Hello Sarbaz Ali !!!";
?>
</h1>
</body>
</html>
But when the file is look like this, then it will work:
<?php
echo "<h1> Hello Sarbaz Ali !!! </h1>";
?>
Can I encrypt the above file (with HTML tags) using phpBolt?
Depending on the Laravel-Source-Encrypter package, which is based on phpBolt encoder:
The blade files are not real PHP files. They mostly contain HTML and CSS codes.
Blade files are HTML code with PHP code, so there is no meaning in encrypting HTML code because there is no logic inside it.
phpBolt can only decrypt-and-execute php files (as a single indivisible step), it is not going to be possible to decrypt them again.
You can see also: Can you tell me the how we can encrypt blade file?
I would suggest that write your php code in seperate file , encrypt it with php_bolt and then include that file using PHP include function.
So with your help I have been able to assemble the below.
$dir = "./reporting/live-metrics/";
$des = "./reporting/historic-metrics/";
$ctime = time();
foreach (glob($dir."*") as $file) {
$live = file_get_contents($file);
if (strpos($live, 'CORO') === false && filemtime($file) < time() - 1 * 10) {
$exclude[] = $live;
$lines = file( $file , FILE_IGNORE_NEW_LINES );
$lines[3] = 'Taken Down';
$lines[5] = $ctime;
file_put_contents( $file , implode( "\n", $lines ) );
if (!file_exists($des.basename($file).PHP_EOL)) {
mkdir($des.basename($file), 0777, true);
}
rename($file,$des.$ctime);
}
}
My issue is that I am attempting to move the file to the new directory created but I am having a little issue with it. No matter what I do I can only get it to move to $des, i cant seem to get ti to move to the dynamicaly created directory for each specific file. I am assuming it has to do with the fact I am not using rename to its correct params. Below are the some of the combinations I have tried to get it to rename and move.
rename($file,$des.basename($file).PHP_EOL.$ctime); //doesn't move or rename
rename($file,$des.basename($file).$ctime); //adds to historic-metrics/ as jason1465519298
I also tried creating a function and setting the rename to call on that. eg.
$path = $des.basename($file).PHP_EOL;
rename($file,$path.$ctime);
Currently the script is great up until the moving the file. It will move it to ./reporting/historic-metrics/ but I would like it to move to the directory just created. EG, if the file it is currently handeling is called 'Jason' then it will create ./reporting/historic-metrics/Jason but move the file to ./reporting/historic-metrics/
There seems to be two possibilities:
Source or destination file paths may be wrong, you can print and check
The newly created destination directory is getting the
correct permission & ownership.
Otherwise your script looks OK.
I finally got it. My main issue was trying to get the filepath to send the file to. After a few interruptions and rethinking my approach I came up with the below. I know it isn't pretty or as slim as could be made but it does the job perfectly.
$dir = "./reporting/live-metrics/";
$des = "./reporting/historic-metrics/";
$ctime = time();
foreach (glob($dir."*") as $file) {
$live = file_get_contents($file);
if (strpos($live, 'CORO') === false && filemtime($file) < time() - 1 * 10) {
$exclude[] = $live;
$lines = file( $file , FILE_IGNORE_NEW_LINES );
$lines[3] = 'Taken Down';
$lines[5] = $ctime;
file_put_contents( $file , implode( "\n", $lines ) );
if (!file_exists($des.basename($file).PHP_EOL)) {
mkdir($des.basename($file), 0777, true);
}
$user = basename($file); //Gets file name that was used in mkdir
$path = (String) $des.$user."/"; //Compiles variables into string
rename($file,$path.$ctime);
}
}
I am new to php and trying to learn how to navigate a local file structure in for the format:
-Folder
-SubFolder
-SubSubFolder
-SubSubFolder
-SubFolder
-SubSubFolder
...
From another stackoverflow question I have been able to use this code using scandir():
<?php
$scan = scandir('Folder');
foreach($scan as $file)
{
if (!is_dir($file))
{
$str = "Folder/".$file;
echo $str;
}
}
?>
This allows me to generate a list of strings of all the 'SubFolder' in my folder directory.
What I am trying to do is list all the 'SubSubFolder' in each 'SubFolder', so that I can create a string of the 'SubSubFolder' name in combination with its 'SubFolder' parent and add it to an array.
<?php
$scan = scandir('Folder');
foreach($scan as $file)
{
if (!is_dir($file))
{
$str = "Folder/".$file;
//echo $str;
$scan2 = scandir($str);
foreach($scan2 as $file){
if (!is_dir($file))
{
echo "Folder/SubFolder/".$file;
}
}
}
}
?>
This however isn't working, and I wasn't sure if it was because I cannot do consecutive scandir() or if I cannot use $file again.
There is probably a better solution, but hopefully the following will be of some help.
<?php
function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = #opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "<strong>$spaces -$file</strong><br />";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
//To list folders names only and not the files within comment out the following line.
echo "$spaces $file<br />.";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
getDirectory( "folder" );
// Get the current directory
?>
I am currently trying to make a script that will find images with *.jpg / *.png extensions in directories and subdirectories.
If some picture with one of these extensions is found, then save it to an array with path, name, size, height and width.
So far I have this piece of code, which will find all files, but I don't know how to get only jpg / png images.
class ImageCheck {
public static function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output.
$dh = #opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "<strong>$spaces $file</strong><br />";
ImageCheck::getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
echo "$spaces $file<br />";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
}
I call this function in my template like this
ImageCheck::getDirectory($dir);
Save a lot of headache and just use PHP's built in recursive search with a regex expression:
<?php
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+(.jpe?g|.png)$/i', RecursiveRegexIterator::GET_MATCH);
?>
In case you are not familiar with working with objects, here is how to iterate the response:
<?php
foreach($Regex as $name => $Regex){
echo "$name\n";
}
?>
I'm getting following warning:
Warning: file_get_contents(C:\xampp\htdocs\test/wp-content/themes/test\images) [function.file-get-contents]: failed to open stream: Permission denied in ..\plugins\theme-check\main.php on line 29
The line 29 of main.php reads as:
$other[$filename] = file_get_contents( $filename );
Here is code related to $files:
$files = listdir( $theme );
$files = array_merge( listdir( $parent ), $files );
if ( $files ) {
foreach( $files as $key => $filename ) {
if ( substr( $filename, -4 ) == '.php' ) {
$php[$filename] = php_strip_whitespace( $filename );
}
else if ( substr( $filename, -4 ) == '.css' ) {
$css[$filename] = file_get_contents( $filename );
}
else {
$other[$filename] = file_get_contents( $filename );
}
}
// run the checks
$failed = !run_themechecks($php, $css, $other);
As far I have understood, its the permission error. As the file can't seem to access that folder. I'm using XAMPP on Windows 7. I dont know how can i change the folder permissions on windows.
PS. Please notice the folder path in the warning, it has \ and also /.
I don't want to turn off the Warning etc., instead want to fix the warning.
Marc B hit it on the head. You need to add a test to check if the filename is a directory with php function is_dir($filename)
if ( $files ) {
foreach( $files as $key => $filename ) {
if ( substr( $filename, -4 ) == '.php' ) {
$php[$filename] = php_strip_whitespace( $filename );
}
else if ( substr( $filename, -4 ) == '.css' ) {
$css[$filename] = file_get_contents( $filename );
}
else {
if(!is_dir($filename)) $other[$filename] = file_get_contents( $filename );
}
}
Edit
If you are doing something like a directory view online. You could go further and include the directories in a seperate array and sort them and list them out first. and then list the files. I have created something like this for a dynamic gallery.
Try using this PHP function to replace the windows path: getcwd().
You will need to concatenate the file names.