List Directories in Json File - php

I can list all the images from a folder in json using the code below:
<?php
$dir = "images/friday/";
$images = glob($dir."*.jpg");
foreach($images as $image){
$registro = array(
"FILE" => $image
);
$retorno[] = $registro;
}
$retorno = json_encode($retorno);
echo $retorno;
?>
I think it would work with directories too but nothing is displayed if I donĀ“t use print_r($dirs).
<?php
$path = "images";
$dirs = glob($path . "/*", GLOB_ONLYDIR);
foreach($dirs as $dir){
$reg = array(
"FOLDER" => $dir
);
$return[] = $reg;
}
$return = json_encode($return);
echo $return;
?>
echo $return does not display anything. Is there a simple workaround or I have to build something like this function? I need the "FOLDER" identifier.

Related

PHP how to access file from folder and how to download them

<?php
$dir_path = "./folder/";
if(is_dir($dir_path))
{
$files = opendir($dir_path);
{
if($files)
{
while (($file_name = readdir($files)) !== FALSE)
{
if ($file_name != '.' && $file_name != '..'){
echo "".$file_name."<br>";
#echo "<img src=".$file_name.">";
}
}
}
}
}
?>
Returns an array of files and directories from the directory . ... I wanted to easely access data in a certain directory using foreach. I came up with the following:
but it is not download
it say like this
Object not found!
In these cases first you need get files of directory like following:
$dir = './FILE_FOLDER_NAME';
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
Why we used unset, these code remove . and .. from $files variable and you have just file names.
Now you can show files with this approach:
foreach($files as $key => $value):
$path_info = pathinfo($value); //RETURN FILE EXTENTION
?>
<a href="DIRECTORY_PATH<?php echo $value; ?>" target="_blank"><?php echo $value; ?>
<?php
endforeach;
If you want to delete a file can add new button to your foreach like this:
<button href="PHPSAMPLEFILE.PHP?file=<?php echo base64_encode ($value); ?>"><?php echo 'DELETE'; ?></button>
and in your PHP file:
$file = base64_decode($_GET['file']);
$path = './DIRECTORY_PATH/'.$file;
unlink($path);

Display a multi image(array) name from database in codeigniter

My problem is how to display the array image from database. I called that array image because when I uploaded that image I'm using an array to do upload to database.
I'm using this code to insert the file name to database
public function multipost()
{
$filepath = $this->upload->get_multi_upload_data('file_name');
$filenames = '';
foreach($filepath as $a) {
$filenames .= $a['file_name'].",";
}
$filenames = rtrim($filenames,',');
$db_data = array(
'pic_id'=> NULL,
'ad_pic'=> $filenames,
);
$this->db->insert('technical_slide_img', $db_data);
}
That result to
As you can see the ad_pic column has the value of 1.jpg,2.jpg.
if I'm using like this
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode("/", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
to display that image. Is there any way to display that images? Or do I need to separate that 2 images into row?
replace
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode("/", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
with
<?php
foreach ($this->header_model->getalldata() as $row)
{
$image_arr = explode(",", $row->ad_pic);
foreach($image_arr as $image_name)
{
echo base_url().'images/'.$image_name .'<br />';
}
}
?>
As i see in your database table image is saved with comma , and you are exploding it with slash / . So i think you have to replace / with , in your explode function.
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode(",", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
<?php
$image_arr = explode(",", $data_user->image);
foreach($image_arr as $image_name)
{?>
<img style="width:5em;"src="<?php echo base_url().'uploads/'.$image_name?>" class="img-reponsive thumbnail">
<!-- echo base_url().'images/'.$image_name .'<br />'; -->
<?php }
?>

Get all the files from a directory(on a shared path) and save them in wordpress uploads folder

There is a directory on shared path, I'm scanning the directory for files, now what i need is to save each of this file in a single subfolder in uploads folder of wordpress. And insert the same post and attachments details of each file saved into db.
/*Function to List all the folders*/
function listfolders($dir) {
static $alldirs = array();
$dirs = glob($dir . '/*', GLOB_ONLYDIR);
if (count($dirs) > 0) {
foreach ($dirs as $d) $alldirs[] = basename($d);
}
foreach ($dirs as $dir) listfolders($dir);
return $alldirs;
}
$folder_list = listfolders('D:\Base Folder');
/***Saving each FOLDER as TAG in wp_bdqueries_tags Table***/
foreach($folder_list as $folder_name){
$folder_slug = strtolower(preg_replace('/\s+/', '-', $folder_name));
echo $folder_name.'***'.$folder_slug.'<br>';
/*global $wpdb;
$wpdb->insert("wp_bdqueries_tags", array(
"tag_group_id" => 27,
"tag_name" => $folder_name,
"tag_slug" => $folder_slug,
"tag_status" => 'approved',
"tag_description" => '',
"tag_postedby" => 0 ,
"tag_moderatedby" => 0,
"tag_addedon" => date('Y-m-d h:i:s')
));*/
}
/***Listing all the Files and thier FOLDERS/TAGS***/
$directory = 'D:\Base Folder';
$allfiles = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid()) {
if (!$it->isDot()) {
$SubPath = $it->getSubPath();
$tags = explode("\\", $SubPath);
echo "Tags: ";
foreach ($tags as $tag) {
if($tag !== end($tags)) echo $tag.' | ';
else echo $tag;
}
$path = $it->key();
echo $path;
echo '<br>Filename: <a href="'.$path.'" alt="Download" target="_blank" title="Download" >'. basename($path) . "</a><br>";
$allfiles[] = basename($path);
echo "<br>";
}
$it->next();
}
echo '<ol>';
foreach ($allfiles as $file) {
echo '<li>'.$file.'</li>';
}
echo '</ol>';
Using RecursiveIteratorIterator() function mentioned above i was able to retrieve all the files info from the Repository.
Then using copy() method of php by passing the upload directory path and the target destination path i was able to copy/uploads the repository file to my custom uploads folder.

instagram tag api function

Can someone help me fix this code? result only appear 1 picture..
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
$images[] = array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
);
return "<a href='".$url."' target='_blank'><img src='".$thumb."' border='0'/></a>";
}
}
}
What I'm trying to achieve is make all result appear in a page
In your code, you are getting only one image because, you are returning image inside loop so it dont executes all the records inside loop. Try below code
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
$returnval = '';
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
$images[] = array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
);
$returnval .= "<a href='".$url."' target='_blank'><img src='".$thumb."' border='0'/></a>";
}
}
return $returnval;
}
The way you are combining the images array is not concatenating the images. To do so, use the array_merge() function. Below is the result:
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
array_merge($images, array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
));
}
foreach ($images as $image) {
echo "<a href='".$image->$url."' target='_blank'><img src='".$image->$thumb."' border='0'/></a>";
}
}
In addition, the return statement would only execute once. In order to print "all" images, use a foreach loop for the $images array.

php directory iterator, shuffle images

<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach ($dir as $file)
{
$images [] = array (
$file->getPathname() . "\n";
$file->getFilename(). "\n";
);
}
?>
shuffle($images);
Can you help me with the code above? I want to add images to an array using DirectoryIterator, and then shuffle images to generate randomized images. Thank you for your valuable inputs.
This depends on what you are doing with the images, and what path your in at the time, but the following will put their full pathname into an Array.
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach($dir as $file){
$images[] = $file->getPathname();
}
shuffle($images); $imgs = '';
foreach($images as $v){
$imgs .= "<img scr='http://{$_SERVER['SERVER_NAME']}/$v' alt='randomImage' />";
}
//put the below wherever you want in your code
echo $imgs;
?>

Categories