I have a shell script that is created inside a php script (given full permissions). When I try to run the shell script from Terminal, I get no errors, but the script does not run (none of the commands execute). Although, once I copy the contents of the shell script, paste them into a new file in XCode, and overwrite the old shell script, it runs properly.
Any suggestions? I've been trying to figure this out for a very long time now with no progress.
I'm assuming that there is an issue with writing the shell script from the php script since it works when written in XCode or a text editor.
Here is the php script that writes the shell script:
<code>
$filePath = "/Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/irShell.sh";
$script = fopen($filePath, 'w+');
chmod($filePath, 0777);
fwrite($script,"#!/bin/sh");
$irPath = "/Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library"; //path to .ir files
$modPath = "/Applications/MAMP/htdocs/php/Batch/modulator";
if($dir = opendir($irPath)){
while(($file = readdir($dir)) !== false){
$posA = strpos($file, ".IR");
$posB = strpos($file, ".ir");
$posC = strpos($file, ".Ir");
if ($posA == true){
$fileName = trim($file, ".IR");
$noT = substr_replace($fileName, "", 0, 1);
echo "$noT\n";
fwrite($script, "\r" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR");
}
else if ($posB == true){
$fileName = trim($file, ".ir");
$noT = substr_replace($fileName, "", 0, 1);
echo "$noT\n";
fwrite($script, "\r" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".ir");
}
else if ($posC == true){
$fileName = trim($file, ".Ir");
$noT = substr_replace($fileName, "", 0, 1);
echo "$noT\n";
fwrite($script, "\r" . $modPath . "./mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".Ir");
}
}
}
</code>
And here's an example of a shell script that is generated by this php:
#!/bin/sh
/Applications/MAMP/htdocs/php/Batch/modulator/mod -o /Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/codes/1294 T1294.ir
/Applications/MAMP/htdocs/php/Batch/modulator/mod -o /Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/codes/1295 T1295.ir
/Applications/MAMP/htdocs/php/Batch/modulator/mod -o /Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/codes/1296 T1296.ir
Change all the \r in your fwrite statements to \n and move them to the end (or add a final fwrite($script, "\n"); at the end right before you close the file (I don't see a fclose(), by the way).
fwrite($script,"#!/bin/sh" . "\n");
...
fwrite($script, $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR" . "\n");
...
fclose($script);
or
fwrite($script,"#!/bin/sh");
...
fwrite($script, "\n" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR" . "\n");
...
fwrite($script, "\n");
fclose($script);
Shell scripts should have newlines for line endings rather than carriage returns.
Related
I have been trying to modify an XML document within an archived .tgz file by using the PharData class using php. I have no problem modifying the XML through DomDocument class, but I am having a hard time saving the whole process: I end up saving 2 different .phar documents alltogether, w/o modifying anything in the .tgz archive :
folder example image
Here is the code I have been using :
$phar = new PharData($path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file);
echo $phar . "\n";
$dom = new DOMDocument();
$dom->load ('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '/' . substr($file, 0, -4) . '.xml');
if ($dom === null) continue;
$ids = $dom->documentElement->getElementsByTagName('id');
foreach ($ids as $i_id) {
if ($i_id->nodeValue !== $id) continue;
echo $i_id->nodeValue . "\n";
echo $i_id->nodeName . "\n";
$sensor = $i_id->parentNode;
echo $sensor->nodeName . "\n";
foreach ($sensor->childNodes as $object) {
if ($object->nodeName !== 'type') continue;
echo $object->nodeName . "\n";
echo $object->nodeValue . "\n";
$object->nodeValue = $type;
echo $object->nodeValue . "\n" . "\n";
}
}
$dom->save('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '.phar' . '/' . substr($file, 0, -4) . '.xml');
Im using script to go through content of zipped files and the script just misses some of them
We have multiple ZIP archives on a server where invoices (Flat files) are stored. Im using following code to search though them:
foreach($zip_files as $zip_file){
echo $zip_file . "[" . $counter . "/" . $count . "]\n";
$counter++;
//Opening and reading zip file
$zip_content = zip_open($archive_path . "\\" . $zip_file);
while($file = zip_read($zip_content)){
$filename = zip_entry_name($file);
if(strpos($filename,".rcv")){
$content = zip_entry_read($file,4086);
if(strpos($content, "<MvxEnvelope>")){
if(strpos($content, "<Field>TETINR</Field>")){
$type = "TETINR";
$TINR_position = strpos($content, "<Field>TETINR");
$DIVI_position = strpos($content, "<Field>DIVI");
$TINR = substr($content,($TINR_position + 28), 10);
$DIVI = substr($content,($DIVI_position + 26), 3);
fwrite($map, $zip_file . ";" . $filename . ";" . $DIVI . ";" . $TINR . ";" . $type . "\n");
}
}
}
}
}
I would like the script to go through all files and not to miss some of them. Many thanks in advance
Currently I am working on a media uploader system.
When a user uploads files, I wanted the system to automatically check whether this file already exists, and if so, add a (x) to the basename.
DSC_2193.jpg would become DSC_2193 (1).jpg
DSC_2193 (2).jpg would become DSC_2193 (3).jpg
So I came up with the following function:
<?php
private function setFileName($path)
{
if (file_exists($path)) {
$file = pathinfo($path);
if (preg_match('/\(([0-9]+)\).' . $file['extension'] . '/', $file['basename'], $matches)) {
return $this->setFileName($file['dirname'] . '/' . preg_replace_callback('/\(([0-9]+)\).' . $file['extension'] . '/', function($matches)
{
return '(' . ($matches[1] + 1) . ')';
}, $file['basename']) . '.' . $file['extension']);
} else {
$basename = substr(basename($path, $file['extension']), 0, -1);
$basename .= ' (1).' . $file['extension'];
return $this->setFileName($file['dirname'] . '/' . $basename);
}
}
else
return $path;
}
?>
Now I was wondering: would this be the right approach to achieve this? If not: maybe someone can come up with a better alternative. If it is the most efficient way, maybe someone benefits from this.
You can do it easily using while cycle. Here is simple scratch:
private function setFileName($path)
{
$file = pathinfo($path);
if (file_exists($path)) {
$i = 1;
while (file_exists($file['filename'] . ' (' . $i . ').' . $file['extension'])) $i++;
$filename = $file['filename'] . ' (' . $i . ').' . $file['extension']; // don't know if you want also directory path
}
else
$filename = $file['filename'] . '.' . $file['extension'];
// some code to rename/move_uploaded_file...
}
I don't know what you want then if you have $filename set (move_uploaded_file, or just return $filename), so the rest of code is up to you.
I'm trying to rename all files in a folder, I want the last character before the extension to be removed.
This is the code I'm using:
$sql = 'SELECT * FROM tblphotoseries WHERE photoSerieID = ' . mysql_real_escape_string($photoSerieID) . ' AND FKuID = ' . $_SESSION['uID'];
if($series = GetFromDB($sql)){
foreach ($series as $serie){
$path = '../uploads/' . $serie["FKcatID"] . '/' . $_SESSION['uCode'] . '/' . $serie["seriesCode"] . '';
}
$files = scandir($path);
foreach($files as $file) {
if($file != ".." && $file != "."){
$new_name = explode(".", $file);
$ext = $new_name[1];
$new_name = $new_name[0];
$new_name[strlen($new_name) - 1] = "";
$new_name = $new_name . "." . $ext;
$new_name = (string)$new_name;
echo $file . " -> " . $new_name . "\n\r";
clearstatcache();
if(file_exists($path . '/' . $file)){
echo "file_exists \n\r";
clearstatcache();
if(rename($path . '/' . $file, $path . '/' . $new_name)){
echo "rename successful \n\r";
}else{
echo "rename failed \n\r";
}
}
}
}
}
This outputs the following:
AA04a_.jpg -> AA04a.jpg
file_exists
rename failed
AA04b_.png -> AA04b.png
file_exists
rename failed
AA04c_.png -> AA04c.png
file_exists
rename failed
Can anyone spot what's going wrong here? Privileges are ok, I've tried chmod 0777 on the file before renaming as well, without success.
Thanks,
Rik
SOLVED:
$new_name[strlen($new_name) - 1] = "";
had to be replaced by
$new_name = substr_replace($new_name, '', strlen($new_name) - 1, 1);
But what's the best and easiest way to copy a file or folder between a local and remote server using php? These are files located above the web folder, so I'll need to use paths instead of the URL.
I would do it using PHP's built-in FTP functions.
EDIT: Ahh, you want secure. This is what I would use then: SSH2-SFTP
Well i made this function hope it works for you copy files from ftp:
$ftpConnection = the conection, example ftp_connect(1.0.0.1).
$path = the ftp path.
$destination = the local file.
function ftpRecursiveFileListing($ftpConnection, $path, $destination) {
$contents = ftp_nlist($ftpConnection, $path);
foreach ($contents as $currentFile) {
if (strpos($currentFile, '.') === false) {
$dir = basename($currentFile);
echo "<br> <b> Directorio </b>" . $dir;
mkdir($destination . "/" . $dir);
ftpRecursiveFileListing($ftpConnection, $currentFile, $destination . "/" . $dir);
} else {
$file = basename($currentFile);
echo '<br> <b>archivo </b>' . $file;
echo '<br> <b>path </b>' . $path;
echo '<br> <b>completo </b>' . $path . "/" . $file;
ftp_get($ftpConnection, $destination . '/' . $file, $path . '/' . $file, FTP_BINARY);
}
}
}