I am developing an application which will work under file:// URI for safety reasons. It needs to link to other files in my computer as well as read content of some directories.
While reading specific files seems simple, I am unable to find a way to read directory listing. Is there any way to do this.
I have a local web server installed which can be used as a proxy for directory listing. But I ideally don't want to use this approach
Note: I don't want to develop it as Chrome extension, or use a sandbox filesystem. I need to read directory listing of any folder present
Here is a simple program if you have or can install a local web server.
<?php
header('Access-Control-Allow-Origin: null');
$key = "very_long_key";
if (isset($_GET['dir'], $_GET['key']) && is_dir($_GET['dir']) && $_GET['key'] == $key) {
$dir = $_GET['dir'];
$files = glob($dir . '\*.{jpg,jpeg,png,gif,webm,mp4}', GLOB_BRACE);
echo json_encode($files);
} else {
var_dump($_GET);
}
You can use AJAX to make request to it. But still I am looking for a way to do it without need of a web server if anybody knows
Related
i need to backup a site's FTP. The site is hosted on a linux server. The problem is that there is a folder with more than 5k files. Linux can't show me more than 4998 files, so i cant copy these files 'cause the server don't give me more than 4998.
I can't delete these files to see the others 'cause the site is actually online. I can't move these file in another directory for the same reason.
What can i do? i'm trying using a shell...but i don't know...i'm not sure using this method.
I got solution for my own answer
<?php
$rootPath = realpath('wp-content/uploads/2014/07');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('dio.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
You can do this through the command line, this guide shows you how. It seems mget (the ftp command) isn't recommended for recursive calls (subfolders and their content), so wget can also be used, se this.
I also like to zip such folders with many files into one, for easy oversight when up-and-downloading. Use
zip -r myfiles.zip myfiles
Here's a guide for that too.
I have a web application that reads files from its local directory (in wamp/www/). This file needed to be accessed by several users so I synced and shared it using Dropbox. Now, is there a shortcut I can use in php commands such as fwrite such that the code is not strictly applicable to only one computer?
For example, I can't code it to fwrite("C:\Users\name\My Documents\") because that is pretty specific to one user and long. I was wondering if there was a shorthand I can use, like %appdata% or %programfiles%?
Try using
$_SERVER['HOMEDRIVE'] and $_SERVER['HOMEPATH']
For drive and path to user folder respectively
print_r($_SERVER)
Will display all the environment variables. There you can see which one to select.
$fp = fopen("{$_ENV['USERPROFILE']}\My Documents\somefile.txt", 'wb');
See $_ENV on the manual and also getenv().
Please note this will only work in limited circumstances. You can use this internal function instead:
#include<Shlobj.h>
PHP_FUNCTION(win_get_desktop_folder)
{
char szPath[MAX_PATH];
if (zend_parse_parameters_none() == FAILURE)
RETURN_NULL();
if (SUCCEEDED(SHGetSpecialFolderPathA(NULL, szPath,
CSIDL_MYDOCUMENTS, FALSE))) {
RETURN_STRING(szPath, 1);
} else {
RETURN_FALSE;
}
}
I am uploaded a file through the file input. If I print_r($_FILES), I can get several pieces of info, but it doesn't give me the full LOCAL path of the file (the path on my computer, not the server). How do I get this?
I need this to use the FTP library in CodeIgniter. Documentation is here on how to use it to upload a file.
As you can see, it requires the full local path, although I'm not sure why.
As file upload path is a variable which changes from server to server, I would suggest you to use move_uploaded_file() function:
$target = "uploads/myfile.something";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
// do something
}
that way it will always work no matter what the path is even if it changes. The target can be anything you wish, it's relative to the current folder where script is being executed.
I solved this.
public function upload($file, $folder) {
$this->CI->ftp->upload($_FILES['file']['tmp_name'], '/public_html/rest/of/path/' . $_FILES['file']['name'], 'ascii', 0775);
}
The file is being uploaded, so you can access it with $FILES['file']['tmp_name']. This is from within a class, so that's why I'm using $this->CI. Otherwise, I would just use $this.
I am writing a simple file uploader in CodeIgniter 2.0.2. Pasting code below.
Under certain conditions the browser hangs during this upload and I get "waiting for localhost" in the browser status bar (identical behavior in FF and Chrome).
I have verified that the file is being uploaded to the Windows temporary folder (the complete file), but the process gets stuck after that.
It appears that the condition for the bug is file size. But my php.ini has all the right settings for uploading files, and I still get the hang with a 700k file.
This bug occurs only when I run it on Windows 7 Apache, not on an Ubuntu box.
The suggested to me that some paths in the php.ini may be incorrectly set.
Apache log files have not been much help here because there is no error thrown.
I have tried using Chrome developer panel to debug but haven't turned up anything useful.
I am currently trying to get XDebug working, but since no error is thrown and the process doesn't complete, my expectations are low.
Any suggestions for how to trace this bug?
If there are specific php.ini settings you'd like to see, let me know, don't want to do the big dump.
Controller:
function do_upload_sql(){
// create directory
if (! is_dir(BACKUP_DIR)) {
mkdir(BACKUP_DIR, 0777);
}
// or if it exists and has restricted file permissions, change them
elseif(fileperms(BACKUP_DIR)!=0777){
chmod(BACKUP_DIR, 0777);
}
$config['upload_path'] = BACKUP_DIR;
$config['allowed_types'] = 'backup'; // an SQL backup file type
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) // this is the native CI function, probably where the problem is. I can provide some of that code if anyone wants.
{
$data['action'] = 'c_backup_restore/do_upload_sql';
$tab_error = array('error' => $this->upload->display_errors());
$data['error'] = $tab_error['error'];
$this->load->view('common/header');
$this->load->view('v_upload_sql', $data);
}
else
{
echo "success"; // yes it's getting here, but i get no file!
$data = array('upload_data' => $this->upload->data());
$file_upload = $data["upload_data"];
$this->restore_backup($file_upload); // go do something useful with the file
}
}
View:
<p>Select the backup file</p>
<div class="not_success"><?php echo $error;?></div>
<?php echo form_open_multipart($action);?>
<input type="file" name="userfile" size="30" />
<input type="submit" value="Upload" />
</form>
If your error only occurs on windows 7 apache, I think that your issue may be with the "is_dir()" "mkdir()" and "chmod()" commands -- these terminal commands are linux specific and will not work so great on a windows system.
I looked up the documentation on the mkdir() function in the PHP.net Manual at:
http://us.php.net/manual/en/function.mkdir.php
I found this in the comments section:
kendsnyder at gmail dot com 04-May-2007 08:17
When creating directories in Windows, trailing periods (".") are ignored. for example:
<?php
mkdir('c:/Buck Jr.',0755); // on Windows creates "c:/Buck Jr"
mkdir('c:/Elipses...',0755); // on Windows creates "c:/Elipses"
mkdir('c:/php.com',0755); // on Windows creates "c:/php.com"
?>
This is a Window's quirk, not a php shortcoming--meaning that you get the same results from a Window's command prompt.
So it seems it will work... but you are likely to have to futz with it to find the flavor that Windows systems prefer. Also it seems that you must specify the root drive on which to make the directory, e.g.: 'c:\somedir'
Also of interest, I did a quick google search and discovered that PHP has published a bug around the functionality you are using, specifically related to forward vs. back slashes of the windows vs. linux systems:
https://bugs.php.net/bug.php?id=29797
Here is a StackOverflow post related to your question:
PHP mkdir(), chmod() and Windows
So to summarize:
On a windows based web server, include the drive that the directory will be placed on
File Permissions on Windows are different from Linux (Windows doesn't have a chmod or file permissions structure like Linux!) Although from what I can tell Windows is supposed give a folder the equivalent of a 777 permissions level -- but this functionality seems buggy and error prone. But this also means that using the chmod() function on a windows system, you will get unpredictable results. For more info consult the PHP Manual: http://php.net/manual/en/function.chmod.php
Remember to use backslashes rather than forward slashes for the directory path, as Windows systems use back slashes, whereas Linux uses forward slashes.
When PHP creates a directory it uses the permissions that were granted to the user account that PHP is running under, which means that you may have to dig into windows to find what user permissions level your windows 7 Apache/PHP is running under and make that change there. I know, what a pain, right?
One final thing: mkdir() function returns true if the directory creation was successful and false if not -- you may want to be testing for that condition before proceeding with the rest of your code, because if the directory doesn't properly get created, all the rest of your code that relies on that directory existing is going to fail. Ultimately I think your browser is hanging upon submit because it is trying to access a function through PHP which there is no support for on Windows (chmod). Perform the function calls within a "Try/Catch" block to catch any exceptions that occur so that you can print them to screen or a log file if necessary:
$proceed = false;
try
{
// create directory
if (! is_dir(BACKUP_DIR)) {
$proceed = mkdir(BACKUP_DIR, 0777);
}
// or if it exists and has restricted file permissions, change them
elseif(fileperms(BACKUP_DIR)!=0777){
$proceed = chmod(BACKUP_DIR, 0777);
}
}
catch(Exception $e)
{
echo $e->message;
}
if($proceed == TRUE)
{
/*Proceed with your code*/
}
else
{
/*Gracefully fail*/
}
Good luck man, this is why I prefer Linux Boxes for web servers!
Cheers!
I'm currently developing a Web app for the iPad.
So I created the whole "application", with my different html files, my css, my pictures.
Now the next step for me is to be able to cache the files to use the "application" offline.
I follow the advices I found on different websites, with the manifest file and everything.
It seems I'm the only one with this issue, because I searched on Internet an answer to my issue, but I didn't find anything.
So I created my manifest file (ipad.manifest) which looks like this :
$CACHE MANIFEST
$/WebApp/home-start.htm
$/WebApp/accommodation.htm
$/WebApp/accommodation2.htm
$/WebApp/dining.htm
$/WebApp/entertainment.htm
$/WebApp/general.htm
$/WebApp/home.htm
$/WebApp/shopping.htm
$/WebApp/sights.htm
$/WebApp/sports.htm
$/WebApp/css/screen3.css
$/WebApp/Player/CanalVenetian.mp4
$/WebApp/Player/DancingWater.mp4
$/WebApp/Player/NaCha.mp4
$/WebApp/Player/NaCha2.mp4
$/WebApp/Player/Opening.mp4
$/WebApp/Player/previewhome.jpg
$/WebApp/Player/previewsights.jpg
$/WebApp/Player/previewvenetian.jpg
$/WebApp/Player/Venetian.mp4
$/WebApp/Player/video.js
$/WebApp/Player/Zaia.mp4
$/WebApp/iPad/startup.png
$/WebApp/iPad/pixel.gif
$/WebApp/iPad/asktt-ipad-accommodation2.jpg
$/WebApp/iPad/asktt-ipad-camera.jpg
$....
I tried with both, relative and absolute links, and it's still not working.
And in every html files I added :
$<!DOCTYPE html>
$<html manifest="ipad.manifest">
$<head>
When I go to the website with the iPad and click on "add to home screen", it adds the icon on the home screen but didn't download the content. So each time I open the application it starts to load everything....
When I tried on desktop browsers it didn't ask me to cache the content on my computer.
I added an header "content/type : text/manifest" for my mime type files on my server.
But I don't understand why it doesn't cache anything, or doesn't even ask me if I want to cache the files???
Does someone have an idea ? Or had the same issue ?
try with this, put it on the root of your app and name it manifest.php and add it to your html web app with
php file start here
$hashes="";
$dir = new RecursiveDirectoryIterator(".");
foreach(new RecursiveIteratorIterator($dir) as $file){
if ($file->IsFile() &&
$file != "./manifest.php" &&
substr($file->getFilename(),0,1) != ".")
{
echo $file . "\n";
$hashes .= md5_file($file);
}
}
echo "# Hash:" . md5($hashes) . "\n";
?>
php file ends here