I get an error in my php code when trying to get all the files from their directory, then creating html links for them and I don't understand why.
Here is the error:
Warning: printf(): Too few arguments in C:\Users\Ryan\Documents\Web Development\xampp\htdocs\muzik\player.php on line 59
Line 59 is:
printf("<li><a href='mp3/%s'>%s</a></li>", htmlentities($file->getBasename()));
Here is the code:
`echo '<ul id="playlist">';
foreach( new DirectoryIterator('mp3/') as $file) {
if( $file->isFile() === TRUE) {
printf("<li><a href='mp3/%s'>%s</a></li>", htmlentities($file->getBasename()));
}
}
echo '</ul>';`
You have two %s, so the printf expects 2 arguments and you only put one.
You may want to use this one :
$filename = htmlentities($file->getBasename();
printf("<li><a href='mp3/%s'>%s</a></li>", $filename, $filename);
Related
When I try to delete empty directory I got the following warnings for on line 93 and on line 99:
Warning: unlink(/data/sites/web/xxx/www/uploads/file/2021-04-02/.nfs0000000059f70c14000017a9): Device or resource busy in /data/sites/web/xxx/www/dreq/req.php on line 93
Warning: rmdir(/data/sites/web/xxx/www/uploads/file/2021-04-02): Directory not empty in /data/sites/web/xxx/www/dreq/req.php on line 99
here is the delete function :
function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
rrmdir($full); // On line 93
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src); // On line 99
}
What is the issue here?
Issue 1. Check which process hold that file with lsof -f | grep filename to check which process hold that resource.
Issue 2. Check the manual rmdir, the directory must be empty.
Attempts to remove the directory named by dirname. The directory must
be empty, and the relevant permissions must permit this. A E_WARNING
level error will be generated on failure.
Refer to this to has a view on where the hidden nfs file comes from
I have these errors when running my script.
failed to open dir: No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Warning: scandir(): (errno 2): No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Warning: array_diff(): Argument #1 is not an array in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 7
Well this is my code below, I don't understand why it failed to open my dir when it's being declared below? Can someone help me with this.
Code of my word2.php
<?php
$numargs = count($argv);
if ($numargs > 1) {
$folder = $argv[1];
echo "Folder is: " . $folder . "\n";
$files = array_diff(scandir($folder), array('.', '..')); //line 6
foreach ($files as $file) { //line 7
$filename = str_replace("í»", "", $filename);
}
} else {
echo "You need to pass the folder absolute path";
exit();
}
Code for running my script using this command ./run.bat this is the filename with a code below.
php word2.php "/Applications/MAMP/htdocs/sites-store/word/images"
PAUSE
Try changing your .bat file to
php word2.php -- "/Applications/MAMP/htdocs/sites-store/word/images"
PAUSE
and you should also do var_dump($argv), to see how your script gets its parameter.
failed to open dir: No such file or directory in
/Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Make sure that your file in that directory.
Check the filename again means is there any spelling mistakes or duplicated.
Make sure if it's read that directory or not
This question already has answers here:
PHP - Failed to open stream : No such file or directory
(10 answers)
Closed 5 years ago.
I am just trying to make a simple PHP program that allows me to generate my page quickly.
I am completely new at PHP.. And I have no clue what I am doing.
/index.php
<?php
include "/base/startup.php";
echo "Test";
startPage("Home");
?>
I'm getting a 500 server error with this.. Please tell me what I'm doing wrong. Thank you.
/base/startup.php
$HOME = "/";
$SCRIPT = <<<EOD
EOD;
$IMPORTS = array(
"/scripts/script.js"
);
$STYLES = array(
"/styles/style.css"
);
function prnt($string) {
echo $string;
}
function map($func, $arr) {
foreach($arr as $i) {
call_user_func($func, $i);
}
}
function linkScript($script) {
prnt("<script src='$script'></script>");
}
function linkStyle($style) {
prnt("<link rel='stylesheet' href='$style'/>");
}
function startPage($title, $script="", $imports=array(), $styles=array()) {
$pre_tags = array(
"<html>",
"<head>"
);
$post_tags = array(
"</head>",
"<body>"
);
map(prnt, $pre_tags);
prnt("<title>$title</title>");
map(linkScript, $IMPORTS);
map(linkScript, $imports);
map(linkStyle, $STYLES);
map(linkStyle, $styles);
map(prnt, $post_tags);
}
function genNav() {
$nav_links = array(
"Home"=>$HOME,
"Walkthroughs"=>$HOME . "/walkthroughs/",
"Dex"=>$HOME . "dex.php"
);
prnt("<div class='nav'>");
foreach ($nav_links as $key => $value) {
prnt("<a class='link' href='" . $value . "'/>" . $key . "</a>");
}
}
function endPage() {
$endTags = array(
"</body>",
"</html>"
);
}
?>
This is the error:
Warning: include(/base/startup.php): failed to open stream: No such file or directory in /var/www/html/index.php on line 2
Warning: include(): Failed opening '/base/startup.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/index.php on line 2
Test
Fatal error: Uncaught Error: Call to undefined function startPage() in /var/www/html/index.php:4 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 4
Since you mentioned you are on a Linux machine, it looks like the issue is caused because of the / here. The / is considered the root directory of linux machine. So removing the / must most probably work:
<?php
include "base/startup.php"; // Try removing the slash.
echo "Test";
startPage("Home");
?>
Since you haven't enabled the display of errors, the issue would be, there's no /base in your system and it would have thrown an error, like Fatal: Include file not found., which is not displayed because of your configuration, instead it would have shown Error 500 silently.
Update
Along with the above error, after seeing your code, the next one is you need to quote the function names. So replace the stuff with:
map("prnt", $pre_tags);
prnt("<title>$title</title>");
map("linkScript", $IMPORTS);
map("linkScript", $imports);
map("linkStyle", $STYLES);
map("linkStyle", $styles);
map("prnt", $post_tags);
The next error is, you haven't included the global variables correctly inside the function. You need to use:
global $IMPORTS, $STYLES;
Now your code works as expected.
And finally finishing the endPage() function:
function endPage() {
$endTags = array(
"</body>",
"</html>"
);
foreach($endTags as $tag)
echo $tag;
}
After installing CakePHP successfully, on first time running, I'm getting these warnings at the bottom. How can I fix this.
Warning (2): Missing argument 1 for View::element(), called in /Users/michaelanywar/Sites/cakephp/app/View/Layouts/default.ctp on line 61 and defined [CORE/Cake/View/View.php, line 398]
Notice (8): Undefined variable: name [CORE/Cake/View/View.php, line 416]
Notice (8): Undefined variable: name [CORE/Cake/View/View.php, line 422]
Notice (1024): Element Not Found: Elements/.ctp [CORE/Cake/View/View.php, line 425]
My View/view.php lines from 398 to 427 look like this:
public function element($name, $data = array(), $options = array()) {
$file = $plugin = null;
if (isset($options['plugin'])) {
$name = Inflector::camelize($options['plugin']) . '.' . $name;
}
if (!isset($options['callbacks'])) {
$options['callbacks'] = false;
}
if (isset($options['cache'])) {
$contents = $this->_elementCache($name, $data, $options);
if ($contents !== false) {
return $contents;
}
}
$file = $this->_getElementFilename($name);
if ($file) {
return $this->_renderElement($file, $data, $options);
}
if (empty($options['ignoreMissing'])) {
list ($plugin, $name) = pluginSplit($name, true);
$name = str_replace('/', DS, $name);
$file = $plugin . 'Elements' . DS . $name . $this->ext;
trigger_error(__d('cake_dev', 'Element Not Found: %s', $file), E_USER_NOTICE);
}
}
If you look at your first warning/error message it should be clear what the issue is: "Warning (2): Missing argument 1 for View::element()".
Look on line 61 of your default layout View template (/app/View/Layouts/default.ctp). You obviously have a call to $this->element() that isn't passing a template name (hence Cake is looking for "Elements/.ctp").
Make sure you pass a template name to the element() method or remove it from your template. For example, if you want to include the template "View/Elements/site_header.ctp":-
echo $this->element('site_header');
The template just needs to exist in the 'View/Elements' folder. You don't need to pass the '.ctp' extension to the element() method, Cake assumes this.
Make sure you've read the docs on Elements.
Moving the default.ctp to View/Elements folder was the best thing and then calling it element('default');?>
I removed the default.ctp in the layout folder..
OK I'm confused. This code is showing me an error but at same time is doing what I actually needed.
$dir = new DirectoryIterator('./');
foreach ($dir as $filename) {
if (mimes_content_type($filename) == 'txt') {
rename($filename, './txt/'.$filename);
}
}
Warning: rename(txt,./txt/txt.txt): Invalid argument in ./www/session/test.php on line 78
Some advices what i did wrong ?