Use imageGetJson in Redactor with fwrite - php

Basicly what i am trying to accomplish is to get a list of uploaded images from a folder using the editor redactor with the function: imageGetJson
I have managed to get fwrite to add an array in my images.json file. Now the problem is that redactor needs brackets around the input in order to render the images.
So my question is how do i get the brackets around my data?
The upload file i have so far is this:
// This is a simplified example, which doesn't cover security of uploaded images.
// This example just demonstrate the logic behind the process.
// files storage folder
$dir = '../img/uploads/';
$_FILES['file']['type'] = strtolower($_FILES['file']['type']);
if ($_FILES['file']['type'] == 'image/png'
|| $_FILES['file']['type'] == 'image/jpg'
|| $_FILES['file']['type'] == 'image/gif'
|| $_FILES['file']['type'] == 'image/jpeg'
|| $_FILES['file']['type'] == 'image/pjpeg')
{
// setting file's mysterious name
$filename = md5(date('YmdHis')).'.jpg';
$file = $dir.$filename;
// copying
copy($_FILES['file']['tmp_name'], $file);
// displaying file
$array = array(
'filelink' => '/img/uploads/'.$filename,
'thumb' => '/img/uploads/'.$filename,
'image' => '/img/uploads/'.$filename,
);
echo stripslashes(json_encode($array));
$json = stripslashes(json_encode($array)), "\n";
$files = fopen('../img/uploads/images.json','a+');
fwrite($files, $json . "\n");
fclose($files);
}
I have made an array that contains the file location and wrote it to the file images.json
The way i need it to look is like this
[
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
]
Only it adds like this:
[]
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
Hope someone can help me with this because i cant figure it out.
Thanks in advance :D

I found another solution: just change the data.json in a php-file data.php which reads the directories you want to list and then convert to json and echo.
Here is what I use now (demo-version, cause my paypal isn't running yet)
(path should be set according to your settings)
In the page where you load the redactor:
imageGetJson: 'redactor901trial/demo/json/data.php'
$path = 'redactor901trial/demo/json/';
$upload_dir = 'images/';
$handle = opendir($upload_dir);
while ($file = readdir($handle)) {
if(!is_dir($upload_dir.$file) && !is_link($upload_dir.$file)) {
$docs[] = $file;
}
}
sort($docs);
foreach($docs as $key=>$file){
$array[] = array(
'filelink' => $path.$upload_dir.$file,
'thumb' => $path.$upload_dir.$file,
'image' => $path.$upload_dir.$file,
'folder' => 'Folder 5'
);
}
echo stripslashes(json_encode($array));
Hope this helps and it is not too late ;)
Regards
Steven

Related

PHP and HTML to make nice interactive file display page

I've found a good demo code that I was able to make work with what I'm trying to do. But the final product is fairly ugly, and doesn't let me view the files. As far as the "nice" part goes, I have a feeling I should be looking as something more jquery oriented?
When i click on the link for a displayed file, I get a url not found error. The path to the file is incorrect...
This is incorrect and what the script currently tries to navigate to:
http://server/var/www/html/reports/1/Doe_John/2019-04-01/Run_2_Report.pdf
This is correct and works to display the file in the browser:
http://server/reports/1/Doe_John/2019-04-01/Run_2_Report.pdf
Is there a straightforward way to "subtract" out the extra file path portion that is causing the problem? I had to add the {$_SERVER['DOCUMENT_ROOT']} part to make it find the files, but that seems to be what is causing the issue now.
I'm also wanting to sort the files by date, and it's not doing that correctly. The dates are being sorted alphabetically by month. Can this be accomplished with just HTML, or should I again be looking at something like Jquery?
PHP that displays all pdf files in structure:
<?PHP
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
//Test User Vars
$region = "1";
$first_name = "John";
$last_name = "Doe";
function getFileList($dir, $recurse = FALSE)
{
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
// open pointer to directory and read list of files
$d = #dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry/",
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry")
);
if($recurse && is_readable("{$dir}{$entry}/")) {
$retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE));
}
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry",
"type" => mime_content_type("$dir$entry"),
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
$d->close();
return $retval;
}
?>
<h1>List PDF files with links</h1>
<table class="collapse" border="1">
<thead>
<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>
</thead>
<tbody>
<?PHP
$dirlist = getFileList("{$_SERVER['DOCUMENT_ROOT']}/reports/{$region}/{$last_name}_{$first_name}/", TRUE);
foreach($dirlist as $file) {
if($file['type'] != "application/pdf") continue;
echo "<tr>\n";
echo "<td>",basename($file['name']),"</td>\n";
echo "<td>{$file['type']}</td>\n";
echo "<td>{$file['size']}</td>\n";
echo "<td>",date('r', $file['lastmod']),"</td>\n";
echo "</tr>\n";
}
?>
</tbody>
</table>
The second link works because /var/www/html/ is probably the DocumentRoot of your server, if you are using Apache, it is kind of the root of your application.

I have file in folder but how to get to know file extension

In my images folder have file
1_cover.???
2_cover.???
3_cover.???
4_cover.???
5_cover.???
I wanna get file extension 4_cover.???
How to write PHP code
==========
UPDATE
Thanks for all help me,
I can use this code
$images = glob("./images/4_cover.*");
print_r($images);
Is that what you are looking for ?
$info = new SplFileInfo('photo.jpg');
$path = $info->getExtension();
var_dump($path);
PHP Documentation
If you want to look in a directory for files, this might not be the best suited way to do your method but since you don't know what the file-type is, you can do something like this: (all code should be in order from top-bottom)
The directory housing all of your files
$directory = "public/images/headers/*";
The files gathered from the glob function, use print_r($files) to see all of the files gathered for debugging if there's an error going on
$files = glob( $directory );
The file you said you were looking for, if this is from a database you'll replace this data with data from the database
$filename_to_lookfor = '4_cover.';
If statements to check the file types and see if they're existant
$file_types_to_check_for = ['gif', 'jpg', 'png'];
foreach ($file_types_to_check_for as $filetype)
if (in_array( $filename_to_lookfor.$filetype, $files)
echo "This is a {$filetype} file!";
After reading more into glob - I'm not too experienced with it.
You can simply write this line:
if (count($files = glob( 'public/images/4_cover.*' )) != 0) $file = $files[0]; else echo 'No file with extension!';
or
$file = (count($files = glob('public/images/4_cover.*') != 0)) ? $files[0] : 'NO_FILE' ;
I apologize for the quite bad quality code, but that's what OP wants and that's the easiest way I could think to do that for him.
You can use the pathinfo function
$file = "file.php";
$path_parts = pathinfo($file);
$path_parts['extension']; // return => 'php'

Download zip file laravel 5.2

Gettin this error when im trying to download zip file
The file "C:\wamp\www\Petro\public\download/downloads.zip" does not exist
My code
if (\File::exists('download/downloads.zip')) {
$directory = public_path('download');
$success = \File::cleanDirectory($directory);
foreach($checked as $check){
$path = File::where('name',$check)->select('real_path')->first();
$img = \Image::make($path->real_path);
$img->save(public_path('download'). '/'. $check);
}
} else {
foreach($checked as $check){
$path = File::where('name',$check)->select('real_path')->first();
$img = \Image::make($path->real_path);
$img->save(public_path('download'). '/'. $check);
}
}
$files = \File::files('download');
\Zipper::make('download/downloads.zip')->add($files);
$pathtoFile = public_path('download/downloads.zip');
return response()->download($pathtoFile);
The zip is created but i cant download it, what is wrong with my code?
When i use dd($files) after create the zip :
array:4 [▼
0 => "download/batman.jpg"
1 => "download/batmanfamily.jpg"
2 => "download/batwoman.jpg"
3 => "download/daredevil.jpg"
]
There is not zip file but if i check in my local directory the zip is created.
Can you guys give me a hand with this and sorry for the bad english.
As you can see in your error you have / instead of \ this.
The file "C:\wamp\www\Petro\public\download/downloads.zip" does not exist
^
Here
change / in following line to \:
$pathtoFile = public_path('download/downloads.zip');
To
$pathtoFile = public_path('download\downloads.zip');
You need to close the process.
Zipper::make('download/downloads.zip')->add($files)->close();

Change folder to fetch on filemanager

I've the following problem: i'm working on a filemanager consisting in only one index.php file, that fetches all the files and folders from the actual folder there its located in.
So if i have a folder with the filemanager file on it:
Folder01
-folder-A
-folder-B
-file1.php
-image.png
-index.php
The filemanager will show: folder-A, folder-B, file1.php and image.png
The problem is I can't change the folder to fetch it's content, to be able to view, by default, the content of folder-A for example.
$file = isset($_REQUEST['file']) ? urldecode($_REQUEST['file']) : '.';
if(isset($_GET['do']) && $_GET['do'] == 'list') {
if (is_dir($file)) {
$directory = $file;
$result = array();
$files = array_diff(scandir($directory), array('.','..'));
foreach($files as $entry) if($entry !== basename(__FILE__)) {
$i = $directory . '/' . $entry;
$stat = stat($i);
$result[] = array(
'mtime' => $stat['mtime'],
'size' => $stat['size'],
'name' => basename($i),
'path' => preg_replace('#^\./#', '', $i),
'ext' => pathinfo($i, PATHINFO_EXTENSION),
'is_dir' => is_dir($i),
'is_deleteable' => (!is_dir($i) && is_writable($directory)) ||
(is_dir($i) && is_writable($directory) && is_recursively_deleteable($i)),
'is_readable' => is_readable($i),
'is_writable' => is_writable($i),
'is_executable' => is_executable($i),
);
}
} else {
err(412,"Not a Directory");
}
echo json_encode(array('success' => true, 'is_writable' => is_writable($file), 'results' =>$result));
exit;
}
This snippet is the beginning of its php code, which is in charge of fetching the files of the selected folder.
Any idea about how to change that?
try glob function, its like dir command in dos,
it will list all of the dirs and files in the current working directory and put them in an array, which is the return value of the function.

Not allowed to load local resource: file:///

I have some images in a folder called "Slides" in my project; I am trying to access those images; but it gives me the following error:
Not allowed to load local resource: file:///C:/xampp/htdocs/MyProject/Slides/123_completed.jpg
where is "file://" coming from?! (Probably thats the problem!)
this is my method to scan the images in the folder called "Slides"
the following line of code is in my
$this->signage_Path = realpath(APPPATH . '../Slides');
public function get_Signage_Images() {
$files = scandir($this->signage_Path);
$newFiles = array_diff($files, array('.', '..'));
$images = array();
foreach ($newFiles as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == "jpg") {
$images[] = array(
'url' => $this->signage_Path.'\\'.$file,
'thumb_url' => $this->signage_Path.'\\'.$file,
);
}
}
return $images;
}
If you need more clarification, please let me know which part!
Thanks
It's probably coming from your APPPATH constant in realpath(). Should be more like $this->signage_Path = 'MyProject/Slides', without realpath(). PHP cannot access a file that is not on a Server, without Client permission. Of course, I really don't know what your sinage_Path is supposed to be. This should just give you an idea of your problem.

Categories