I have a very simple code, but the final command I can not apply it to the generated file, I can not rename the file and I don't know why. it is not a renaming issue because if I change the renaming for $cmd ="cp '$textfile''$file'" or $cmd ="mv '$textfile''$file'", it does not work either. I better explain it with the code:
<?php
// the original file to work with
$file = "COPYING.odt";
//change the extension of doc|pdf|docx|odt|rtf files to txt
$txtfile = preg_replace('"\.(doc|pdf|docx|odt|rtf)$"', '.txt', $file);
//convert the odt file into txt and name it with the original name but txt extension
$cmd = "/usr/bin/unoconv -f txt '$file' -o '$textfile'";
shell_exec($cmd);
//store the file in the originals files folder
rename("$file", "orig/$file");
//rename the generated txt file with the original name and extension "COPYING.odt"
rename("$textfile", "$file");
echo "conversion of file <em>$file</em> done";
?>
Any suggestions? Thanks.
UPDATE One thing I have forgoten to tell is that I apply this script to an uploaded file, so I can not give the static name of $file, but the variable $file.
UPDATE 2 Following #AlanMachado suggestion I've changed mv command for rename, and this rename works but not the next.
Sorry guys, I've been three days with this script, that's why I ask the question in Stackoverflow, but just now I've realised that it is a TYPO error, yes:
$txtfile = preg_replace('"\.(doc|pdf|docx|odt|rtf)$"', '.txt', $file);
txtfile without e in txt, while
rename("$textfile", "$file");
textfile with e in text, I'm really sorry.
Anyway I don't know why the results of this command was the file with the right name:
$cmd = "/usr/bin/unoconv -f txt '$file' -o '$textfile'";
Related
I have a problem when I want to copy a file from one folder to another on my website, I don't know what I'm doing wrong,the file is called "installed" and dont have extension, is only archive,I need to copy this file from the "ins" folder to the "var" folder this is the php code I'm using:
<?php
$file = '/../domains/travianforce.com.es/public_html/ins/installed';
$new_file = '/../domains/travianforce.com.es/public_html/var/';
if (!copy($file, $new_file)) {
echo "Error to copy $file...\n";
}
?>
but when I run the script it gives me an error, I don't know what I'm doing wrong but I need your help, thank you very much! =)
Give full path in $file and $new_file
And ans already given in this
I am using Zipper to extract uploaded zip file and delete the file after extract.
So I upload and extract like this:
$f = $request['file']->move(public_path($directory), $fullFileName);
\Zipper::make($f)->extractTo(public_path($directory) . $fileName);
and it works great. I've tried to delete the file using these ways.
1 - Storage::disk('products')->delete($fullFileName);
2 - File::delete(public_path($directory) . $fullFileName);
3 - $del = unlink(public_path($directory) . $fullFileName);
but in all actions get resource temporarily unavailable error.
I found this error is because of the zipper (simple files and directories works).
so my question is, How can I delete upload zip file after extract, using a zipper?
Any idea would be great.
thanks in advance.
You need to call $zipper->close(); after you extract it, so if you do something like this it should work:
$zipper = new \Chumper\Zipper\Zipper;
$zipper->make($f)->extractTo(public_path($directory) . $fileName);
$zipper->close();
unlink(public_path($directory) . $fullFileName);
If you do not close the zipper it will not write the result to the disk and keep the original file locked. See the documentation.
$zip = new Zipper;
$zip->make($pathZipFile)->extractTo($destinationPath);
$zip->close(); // important
unlink($pathZipFile); // delete Zip file after
I am creating Epub package which I need to pack in the following order
$dest = "ebooks/pack/";
exec("zip -DX0 ".$dest."/book.zip ".$dest."/mimetype ");
exec("zip -DrX9 ".$dest."/book.zip ".$dest."/META-INF ".$dest."/OEBPS");
So I when I pack this , Zip archive structure looks like below
ebooks/pack/mimetype
ebooks/pack/META-INF
ebooks/pack/OEBPS
But I need that files to be in home , For example when I un compress It should look like below
mimetype
META-INF
OEBPS
I also tried to navigate via cd ebooks/pack/ , that throws the error
zip warning: name not matched: mimetype
I even used full path like home/user/public_html/ful
exec("cd home/sam/public_html/domain.com/".$dest."/; zip -DX0 ".$dest."/book.zip mimetype 2&1",$output);
I even tried chdir , It displays the correct path where my files are there
echo getcwd() . "\n";
chdir('home/sam/public_html/domain.com/".$dest."/');
echo getcwd() . "\n";
We have few fields in the HTML page and this is being written into a file using php.
The file has the data, however, running a bash script which takes this file with a .txt as extension is not working.
When the file is opened and re-saved manually the bash script will work properly!
I've tried changing the permissions of the file but the bash script is still not using this file. Any help on this is greatly appreciated.
PHP Script:
$name = "test.txt";
$handle = fopen($name, "w");
fwrite($handle, "my message");
fclose($handle);
Bash Script:
INPUT="$1"
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read message number ; do
echo $message # or whaterver you want to do with the $line variable
#j=$[$(line)]
echo $number
echo "$message" | gnokii --sendsms $number --smsc $SMSC
done < $INPUT
IFS=$OLDIFS
You probably want to use fclose($handle) rather than unlink()
Edit OK then, "Because..."
fclose($handle) closes the file referenced by the handle: $handle
unlink() takes a file path string as a parameter, rather than a resource.
If used as intended, unlink() will actually delete the file.
unlink() as mentioned would delete the file. Just created a plain script like this
<?php
$name = "/var/www/test.txt";
$handle = fopen($name, "w");
fwrite($handle, "test,9944");
fclose($handle);
?>
This script works f9 with storing the data. But running bash script on this with the file input as test.txt does nothing. But again opening and saving with the same file name and it works.
finally got the answer its the end of file that is missing in text file that is created by php. anyway thank you all for contributing.
I'm using wget in a php script and need to get the name of the file downloaded.
For example, if I try
<?php
system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
?>
I will get a file called index.html in the downloads directory.
EDIT: The page will not always be google though, the target may be an image or stylesheet, so I need to find out the name of the file that was downloaded.
I'd like to have something like this:
<?php
//Does not work:
$filename = system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
//$filename should contain "index.html"
?>
Maybe that's some kind of cheating, but why not :
decide yourself the name of the file that wget should create
indicate to wget that the download should be made to that file
when the download is finished, use that file -- as you already know the name.
Check out the -O option of wget ;-)
For example, running this from the command-line :
wget 'http://www.google.com/' -O my-output-file.html
Will create a file called my-output-file.html.
if your requirement is simple like just getting google.com, then do it within PHP
$data=file_get_contents('http://www.google.com/');
file_put_contents($data,"./downloads/output.html");
On Linux like systems you can do:
system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
$filename = system('ls -tr ./downloads'); // $filename is now index.html
This works if there is no other process creating file in the ./downloads directory.
I ended up using php to find the most recently updated file in the directory using the following code:
<?php
system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
$dir = "./downloads";
$newstamp = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
# Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fn)) continue;
$timedat = filemtime("$dir/$fn");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
}
// $newname contains the name of the most recently updated file
// $newstamp contains the time of the update to $newname
?>