I am building a folder with files in it. At the end, I want to Zip this folder. On my local machine using Homestead, everything works correctly.
However, on my web server I am getting the error:
ZipArchive::close(): Can't remove file: No such file or directory
Why? The folder is filled with all files...
My code:
$zip_file = storage_path('app\\takeouts\\takeout_' . $this->dataExports->uuid . '.zip');
$this->zip = new \ZipArchive();
$this->zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$this->addAllFilesToZipArchive($this->folder_name);
$this->zip->close();
Storage::deleteDirectory($this->folder_name);
private function addAllFilesToZipArchive($dir)
{
$dirs = Storage::directories($dir);
$files = Storage::files($dir);
foreach ($files as $file)
{
if(Storage::exists(storage_path("app\\" . $file))) {
$this->zip->addFile(storage_path("app\\" . $file), str_replace($this->folder_name,"","/" . $file));
}
}
foreach ($dirs as $dir2) {
$this->addAllFilesToZipArchive($dir2);
}
}
It may seem a little obvious to some but it was an oversight on my behalf.
ll the close() function.
If the files added to the object aren't available at save time the zip file will not be created.
I am trying to follow the code on the php docs listed here
http://php.net/manual/en/function.scandir.php
However when I run the code nothing is displayed. Could someone please show me where I am making mistakes?
my code
<?php
$directory = "/xyz/images/";
$images = glob($directory . "*.jpg");
foreach($images as $image)
{
echo $image;
}
error_reporting(E_ALL);
?>
Nothing is being displayed. I am using xampp as on windows.
I have also referenced these questions
Listing all images in a directory using PHP
Get filenames of images in a directory
I have also tried "c:/xampp/htdocs/xyz/images/", " /xyz/images/" , "xyz/images/", "/images/"as my directory.
<?php
error_reporting(E_ALL);
$directory = scandir("linewalk');
$images = glob($directory . "*.jpg");
foreach($images as $image)
{
echo $image;
}
?>
its now throwing errors
Parse error: syntax error, unexpected '.'
<?php
error_reporting(E_ALL);
foreach (scandir(".") as $file)
echo $file . "\n";
?>
****** thanks for all your help and suggestions*********
works and lists content of current dir
I am currently in root/xyz/test
so to scan /xyz/images it should be
(scandir("qa/linewalk")
Warning: scandir(qa/linewalk,qa/linewalk): The system cannot find the path specified - is what I get when I try
You should convert your directory to a file system path:
$directory = dirname(__FILE__) . "/xyz/images/";
Also, you can use RecursiveDirectoryIterator
$dirIterator = new RecursiveDirectoryIterator($directory);
$iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
print_r($file);
}
Line
$directory = "/xyz/images/";
means that you take files from xyz folder in the root of your server (computer). I suppose there's no such folder and you don't have permissions to access it.
So, you should set your path more carefully, for example:
// path comes from yor server docroot
$directory = $SERVER['DOCUMENT_ROOT'] . "/xyz/images/";
// path comes from current folder in which your script is run
$directory = "./xyz/images/";
The path is relative to the position of the php file.
Hence if the php file and the xyz directory are located in the same parent directory, then simply use
$directory=scandir(xyz/images/);
I have coded a PHP script that includes every file in a directory. But im wondering if there is a way to load the classes in the files im including like a autoloader or something?
<?php
define("include_dir", dirname(__FILE__) . '/includes/');
foreach (scandir(include_dir) as $filename)
{
if (is_file(include_dir . '/' . $filename))
{
//its a php file, lets do this!
if (substr($filename, -4) == '.php')
{
include include_dir . $filename;
}
}
}
?>
try this-
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
the manual -
http://php.net/manual/en/language.oop5.autoload.php
Selenium2, by default, starts firefox with a fresh profile. I like that for a default, but for some good reasons (access to my bookmarks, saved passwords, use my add-ons, etc.) I want to start with my default profile.
There is supposed to be a property controlling this but I think the docs are out of sync with the source, because as far as I can tell webdriver.firefox.bin is the only one that works. E.g. starting selenium with:
java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin=not-there
works (i.e. it complains). But this has no effect:
java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.profile=default
("default" is the name in profiles.ini, but I've also tried with "Profile0" which is the name of the section in profiles.ini).
I'm using PHPWebdriver (which uses JsonWireProtocol) to access:
$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");
I tried doing it from the PHP side:
$webdriver->connect("firefox","",array('profile'=>'default') );
or:
$webdriver->connect("firefox","",array('profile'=>'Profile0') );
with no success (firefox starts, but not using my profile).
I also tried the hacker's approach of creating a batch file:
#!/bin/bash
/usr/bin/firefox -P default
And then starting Selenium with:
java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin="/usr/local/src/selenium/myfirefox"
Firefox starts, but not using by default profile and, worse, everything hangs: selenium does not seem able to communicate with firefox when started this way.
P.S. I saw Selenium - Custom Firefox profile I tried this:
java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate "not-there"
And it refuses to run! Excited, thinking I might be on to something, I tried:
java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate /path/to/0abczyxw.default/
This does nothing. I.e. it still starts with a new profile :-(
Simon Stewart answered this on the mailing list for me.
To summarize his reply: you take your firefox profile, zip it up (zip, not tgz), base64-encode it, then send the whole thing as a /session json request (put the base64 string in the firefox_profile key of the Capabilities object).
An example way to do this on Linux:
cd /your/profile
zip -r profile *
base64 profile.zip > profile.zip.b64
And then if you're using PHPWebDriver when connecting do:
$webdriver->connect("firefox", "", array("firefox_profile" => file_get_contents("/your/profile/profile.zip.b64")))
NOTE: It still won't be my real profile, rather a copy of it. So bookmarks won't be remembered, the cache won't be filled, etc.
Here is the Java equivalent. I am sure there is something similar available in php.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
WebDriver driver = new FirefoxDriver(ffprofile);
If you want to additonal extensions you can do something like this as well.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
ffprofile.addExtension(new File("path/to/my/firebug.xpi"));
WebDriver driver = new FirefoxDriver(ffprofile);
java -jar selenium-server-standalone-2.21.0.jar -Dwebdriver.firefox.profile=default
should work. the bug is fixed.
Just update your selenium-server.
I was curious about this as well and what I got to work was very simple.
I use the command /Applications/Firefox.app/Contents/MacOS/firefox-bin -P to bring up Profile Manager. After I found which profile I needed to use I used the following code to activate the profile browser = Selenium::WebDriver.for :firefox, :profile => "batman".
This pulled all of my bookmarks and plug-ins that were associated with that profile.
Hope this helps.
From my understanding, it is not possible to use the -Dwebdriver.firefox.profile=<name> command line parameter since it will not be taken into account in your use case because of the current code design. Since I faced the same issue and did not want to upload a profile directory every time a new session is created, I've implemented this patch that introduces a new firefox_profile_name parameter that can be used in the JSON capabilities to target a specific Firefox profile on the remote server. Hope this helps.
I did It in Zend like this:
public function indexAction(){
$appdata = 'C:\Users\randomname\AppData\Roaming\Mozilla\Firefox' . "\\";
$temp = 'C:\Temp\\';
$hash = md5(rand(0, 999999999999999999));
if(!isset($this->params['p'])){
shell_exec("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\" -CreateProfile " . $hash);
}else{
$hash = $this->params['p'];
}
$ini = new Zend_Config_Ini('C:\Users\randomname\AppData\Roaming\Mozilla\Firefox\profiles.ini');
$path = false;
foreach ($ini as $key => $value){
if(isset($value->Name) && $value->Name == $hash){
$path = $value->Path;
break;
}
}
if($path === false){
die('<pre>No profile found with name: ' . $hash);
}
echo "<pre>Profile : $hash \nProfile Path : " . $appdata . "$path \n";
echo "Files: \n";
$filesAndDirs = $this->getAllFiles($appdata . $path);
$files = $filesAndDirs[0];
foreach ($files as $file){
echo " $file\n";
}
echo "Dirs : \n";
$dirs = array_reverse($filesAndDirs[1]);
foreach ($dirs as $dir){
echo " $dir\n";
}
echo 'Zipping : ';
$zip = new ZipArchive();
$zipPath = md5($path) . ".temp.zip";
$zipRet = $zip->open($temp .$zipPath, ZipArchive::CREATE);
echo ($zipRet === true)?"Succes\n":"Error $zipRet\n";
echo "Zip name : $zipPath\n";
foreach ($dirs as $dir){
$zipRet = $zip->addEmptyDir($dir);
if(!($zipRet === true) ){
echo "Error creating folder: $dir\n";
}
}
foreach ($files as $file){
$zipRet = $zip->addFile($appdata . $path ."\\". $file,$file);
if(!($zipRet === true && file_exists($appdata . $path . "\\". $file) && is_readable($appdata . $path . "\\". $file))){
echo "Error zipping file: $appdata$path/$file\n";
}
}
$zipRet = $zip->addFile($appdata . $path ."\\prefs.js",'user.js');
if(!($zipRet === true && file_exists($appdata . $path . "\\". $file) && is_readable($appdata . $path . "\\". $file))){
echo "Error zipping file: $appdata$path/$file\n";
}
$zipRet = $zip->close();
echo "Closing zip : " . (($zipRet === true)?("Succes\n"):("Error:\n"));
if($zipRet !== true){
var_dump($zipRet);
}
echo "Reading zip in string\n";
$zipString = file_get_contents($temp .$zipPath);
echo "Encoding zip\n";
$zipString = base64_encode($zipString);
echo $zipString . "\n";
require 'webdriver.php';
echo "Connecting Selenium\n";
$webDriver = new WebDriver("localhost",'4444');
if(!$webDriver->connect("firefox","",array('firefox_profile'=>$zipString))
{
die('Selenium is not running');
}
}
private function getAllFiles($path,$WithPath = false){
$return = array();
$dirs = array();
if (is_dir($path)) {
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
if(!in_array($file, array('.','..'))){
if(is_dir($path . "\\" . $file)){
$returned = $this->getAllFiles($path . "\\" . $file,(($WithPath==false)?'':$WithPath) . $file . "\\");
$return = array_merge($return,$returned[0]);
$dirs = array_merge($dirs,$returned[1]);
$dirs[] = (($WithPath==false)?'':$WithPath) . $file;
}else{
$return[] = (($WithPath==false)?'':$WithPath) . $file;
}
}
}
closedir($dh);
}
}
return array($return,$dirs);
}
The Idea is that you give in the get/post/zend parameters P with the name of the profile if not a random wil be created, and he will zip all the files put it in the temp folder and put it in.
I am writing a script and I was looking into providing some sort of a tool that updates the script when the client requests.
Project is simple logic.
Client server sends a request to mother server (which stores the updated scripts in ZIP files) and downloads the .ZIP file from the server.
Client server receives the .ZIP file and unpacks it overwriting the outdated script files.
Can someone give me a basic guide or what do I need to do to implement such function ?
I can use pclzip to create/extract archives but I do not know where to start.
Thanks for any help, appreciated.
You will need to use ftp(using php) to upload
example in joomla we can do the following :
jimport('joomla.client.ftp');
$ftp = JFTP::getInstance($server['ip'], 21, null, $server['user'], $server['password']);
try {
foreach($files as $file) {
$fullpath = JPATH_SITE . $file;
$this->createdirs($ftp, $rootdir . $file);
if($ftp->store($fullpath, $rootdir . $file) == false) {
throw new Exception("Cannot transfer file " . $file);
}
}
foreach($adminfiles as $file) {
$fullpath = JPATH_SITE . $file;
$this->createdirs($ftp, $rootdir . $file);
if($ftp->store($fullpath, $rootdir . $file) == false) {
throw new Exception("Cannot transfer file " . $file);
}
}
}
catch(Exception $e) {
$ftp->quit();
die($e->getMessage());
}
$ftp->quit();