Error when running my php script - php

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

Related

Copy() function can't detect destination path php [duplicate]

Anyone know why this:
<?PHP
$title = trim($_POST['title']);
$description = trim($_POST['description']);
// Array of allowed image file formats
$allowedExtensions = array('jpeg', 'jpg', 'jfif', 'png', 'gif', 'bmp');
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
echo '<div class="error">Invalid file type.</div>';
}
}
}
if (strlen($title) < 3)
echo '<div class="error">Too short title</div>';
else if (strlen($description) > 70)
echo '<div class="error">Too long desccription.</div>';
else {
move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
}
Gives:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\wamp\www\upload.php on line 41
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php1AB.tmp' to 'c:\wamp\www\uploads\images/' in C:\wamp\www\upload.php on line 41
It's because you're moving a file and it thinks you're trying to rename that file to the second parameter (in this case a directory).
it should be:
move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:/wamp/www/uploads/images/'.$file['name']);
You are specifying to move a file to a directory; neither PHP's move_uploaded_file nor its copy is as smart as a shell's copy -- you have to specify a filename, not a directory, for the destination.
So, one simple solution would be to take the basename of the source file and append that to the destination directory.
It sounds like the second argument to move_uploaded_file should be a full file name instead of just the directory name. Also, probably only a style issue, but you should use consistent slashes in 'c:\wamp\www\uploads\images/'
Because PHP is not a shell. You're trying to copy the file into the c:\wamp\www\uploads\images directory, but PHP doesn't know you mean that when you execute (within the move_uploaded_file function):
copy($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
This command tells it to rename the file to c:\wamp\www\uploads\images/, which it can't do because that's the name of an existing directory.
Instead, do this:
move_uploaded_file($_FILES['userfile']['tmp_name'],
'c:\wamp\www\uploads\images/' . basename($_FILES['userfile']['tmp_name']));
if you want just copy the file in two Dir different, try this :
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target.$pic1))
{
copy("C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/".$pic1, "C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/thumbs/".$pic1)
}
You should write the complete path "C:/..."
Try adding the extension to the name file.
$filename = $_FILES['userfile']['tmp_name'].".jpg";
It will be like below in phalcon 3.42
if ($this->request->hasFiles() == true) {
// Print the real file names and sizes
foreach ($this->request->getUploadedFiles() as $file) {
//Print file details
echo $file->getName(), " ", $file->getSize(), "\n";
//Move the file into the application
$file->moveTo($this->config->application->uploadsDir.$file->getName());
}
}
FIRSTLY FIND YOUR CURRENT DIRECTORY echo getcwd(); IF YOU FIND THE CURRENT DIRECTORY SO MOVE THEM ANOTHER DIRECTORY
FOR EXAMPLE = echo getcwd(); these output is "your live directory name" like that -> /opt/lampp/htdocs/product and create new directory anywhere but your current directory help to go another directory ***
plz read carefully very thinking answer

PHP Function rename fails “No such file or directory"…

Rename Functions fails. i already tried a lot of different solutions of this forum but nothing helps.
This is my Code:
$dirname='rechnungen/';
$handleu=opendir($dirname);
while ( $ufile = readdir ( $handleu ) ) {
$ufile = substr($ufile,11);
foreach ($mydata as $nr => $uinhalt)
{
$ufilename = $uinhalt['File'].'.pdf';
if($ufilename == $ufile) {
rename($dirname.$ufile, $dirname.'ER'.$uinhalt['Zahl'].'_'.$ufile );
}
}
}
closedir( $handleu );
I get this warnings:
Warning: rename(rechnungen/REGW-2020-143.pdf,rechnungen/ER013_REGW-2020-143.pdf): No such file or directory in /volume1/web/sevdesk/rechnungen.php on line 162
Warning: rename(rechnungen/REGW-2021-011.pdf,rechnungen/ER011_REGW-2021-011.pdf): No such file or directory in /volume1/web/sevdesk/rechnungen.php on line 162
Warning: rename(rechnungen/REGW-2021-014.pdf,rechnungen/ER015_REGW-2021-014.pdf): No such file or directory in /volume1/web/sevdesk/rechnungen.php on line 162
Warning: rename(rechnungen/REGW-2021-009.pdf,rechnungen/ER012_REGW-2021-009.pdf): No such file or directory in /volume1/web/sevdesk/rechnungen.php on line 162
Warning: rename(rechnungen/REGW-2021-007.pdf,rechnungen/ER014_REGW-2021-007.pdf): No such file or directory in /volume1/web/sevdesk/rechnungen.php on line 162
Can you help me with that?
try to change the paths in ../rechnungen/file.pdf,
may you have to use the full path and you have to give the files and directories full permissions.
$path = "/volume1/web/sevdesk/";
$dirname= $path.'rechnungen/';

PHP open txt file and echo its contents

The path of my .txt file is C:\Users\George\Desktop\test.txt
And I use:
$path = "C:\\Users\\George\\Desktop\\test.txt";
$fileContent = file_get_contents($path);
echo $fileContent;
But I get file_get_contents(C:\Users\George\Desktop\test.txt) [function.file-get-contents]: failed to open stream. But why?
Use this code:
$path = "C:/Users/George/Desktop/test.txt";
$fileContent = file_get_contents($path);
echo $fileContent;
as the error mentions, that path exists...
Wrong assertion, PHP is just telling you there was an error opening that path, it doesn't mean it exists, also, the error message should mention the reason for the error, i.e.: not found, permission denied, etc...
Answer:
Your code syntax is correct. The error is one of the following 3:
1 - The file doesn't exist.
2 - The path is wrong.
3 - Php doesn't have permission to access that file.
Maybe if YOU do not have the right to read this file as PHP, the system have it, try exec or system :
$path = 'C:\\Users\\George\\Desktop\\test.txt';
function getFile_exec($path)
{
if(file_exists($path))
{
return exec("cat $path");
}
else
{
return false;
}
}
function getFile_syst($path)
{
if(file_exists($path))
{
return system("cat $path");
}
else
{
return false;
}
}
var_dump(getFile_exec($path));
var_dump(getFile_syst($path));
But as you have this error : Warning: file_get_contents(C:/Users/George/Desktop/test.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/a2133027/public_html/index.php on line 5 and like Pedro Lobito said, you must be on linux, so, the good way to access your files on desktop may be : ~/Desktop/test.txt or ~/test.txt if it's directly in your user files ... Are you sure, you are under windows ?

move_uploaded_file() fails to open stream due to invalid argument - PHP

I have this code to move my uploaded file to a specific directory:
if (isset($_FILES["image"]["name"])){
if (!is_dir('pf/' . $uid)) {
mkdir('pf/' . $uid);
$large_image_location = "pf/" . $uid;
}else {
$large_image_location = "pf/" . $uid;
}
chmod ($large_image_location, 0777);
move_uploaded_file("$userfile_tmp", "$large_image_location/$userfile_tmp");
}
However that gives the following error:
( ! ) Warning: move_uploaded_file(pf/BfyhieniKJGGqTNm/C:\wamp\tmp\phpF08A.tmp) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\wamp\www\mingle\upload_dp.php on line 26
Any help on how to sort this out would be greatly appreciated!
This is 90% of your woes:
move_uploaded_file("$userfile_tmp", "$large_image_location/$userfile_tmp");
You using the moved to path at the beginning of the upload path. Try:
move_uploaded_file("$userfile_tmp", "$large_image_location/".$_FILES['image']['name']);
That should work better.
The error itself is pretty clear, pf/BfyhieniKJGGqTNm/C:\wamp\tmp\phpF08A.tmp is not a valid filename.
Don't change the contents of $_FILES[n]['tmp_name'] (or $userfile_tmp for that matter), since it will always contain the full path to the uploaded file.

The second argument to copy() function cannot be a directory

Anyone know why this:
<?PHP
$title = trim($_POST['title']);
$description = trim($_POST['description']);
// Array of allowed image file formats
$allowedExtensions = array('jpeg', 'jpg', 'jfif', 'png', 'gif', 'bmp');
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
echo '<div class="error">Invalid file type.</div>';
}
}
}
if (strlen($title) < 3)
echo '<div class="error">Too short title</div>';
else if (strlen($description) > 70)
echo '<div class="error">Too long desccription.</div>';
else {
move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
}
Gives:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\wamp\www\upload.php on line 41
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php1AB.tmp' to 'c:\wamp\www\uploads\images/' in C:\wamp\www\upload.php on line 41
It's because you're moving a file and it thinks you're trying to rename that file to the second parameter (in this case a directory).
it should be:
move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:/wamp/www/uploads/images/'.$file['name']);
You are specifying to move a file to a directory; neither PHP's move_uploaded_file nor its copy is as smart as a shell's copy -- you have to specify a filename, not a directory, for the destination.
So, one simple solution would be to take the basename of the source file and append that to the destination directory.
It sounds like the second argument to move_uploaded_file should be a full file name instead of just the directory name. Also, probably only a style issue, but you should use consistent slashes in 'c:\wamp\www\uploads\images/'
Because PHP is not a shell. You're trying to copy the file into the c:\wamp\www\uploads\images directory, but PHP doesn't know you mean that when you execute (within the move_uploaded_file function):
copy($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
This command tells it to rename the file to c:\wamp\www\uploads\images/, which it can't do because that's the name of an existing directory.
Instead, do this:
move_uploaded_file($_FILES['userfile']['tmp_name'],
'c:\wamp\www\uploads\images/' . basename($_FILES['userfile']['tmp_name']));
if you want just copy the file in two Dir different, try this :
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target.$pic1))
{
copy("C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/".$pic1, "C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/thumbs/".$pic1)
}
You should write the complete path "C:/..."
Try adding the extension to the name file.
$filename = $_FILES['userfile']['tmp_name'].".jpg";
It will be like below in phalcon 3.42
if ($this->request->hasFiles() == true) {
// Print the real file names and sizes
foreach ($this->request->getUploadedFiles() as $file) {
//Print file details
echo $file->getName(), " ", $file->getSize(), "\n";
//Move the file into the application
$file->moveTo($this->config->application->uploadsDir.$file->getName());
}
}
FIRSTLY FIND YOUR CURRENT DIRECTORY echo getcwd(); IF YOU FIND THE CURRENT DIRECTORY SO MOVE THEM ANOTHER DIRECTORY
FOR EXAMPLE = echo getcwd(); these output is "your live directory name" like that -> /opt/lampp/htdocs/product and create new directory anywhere but your current directory help to go another directory ***
plz read carefully very thinking answer

Categories