I am attempting to read a open a directory using opendir(), and then loop through the files in that directory. The ultimate goal for this script is to transfer certain files from the directory that is being read to a different FTP server.
The "FTP" part is what's screwing me up—and I have no idea why!
On some servers (this script is expected to be run across several) I am receiving the following error:
Warning: opendir(ftp://...#jslsolutions2.flashsvr.com/streaming)
[function.opendir]: failed to open dir: FTP server reports 550
Can't find file in [PATH] on line 88
[PATH] would be the actual path to the PHP script, of course.
Line 88 is as follows:
if ($handle = opendir($from))
The variable $from represents a complete FTP path.
The complete FTP path is as follows:
ftp://[USERNAME]:[PASSWORD]#[SERVER]/streaming
I have confirmed that directory is correct. When I copy and paste the directory into my web browser, for example, Firefox opens a directory tree containing all of the files, exactly as expected. In other words, though PHP is complaining that it "can't find file", the "file" (that is the directory, I believe) actually does exist.
What on earth is going on? As I mentioned before, this script does work on some servers. The thing holding it up appears to be whether or not the server is Windows-based, as it works fine on the Linux servers I have tested. Unfortunately, I do not have access to the servers, though our server administrator can make changes if we can isolate the problem.
Why am I receiving this error, and is there a workaround?
Add a trailing slash / see if that resolves the issue there was a bug in PHP which has been fixed.
Related
Sorry if this a repeat of a question that has been asked before but I have not been able to find my exact situation. We are trying to migrate our website server from a Windows 10 VM (yes I know) to a Windows Server 2019 VM. We have some PHP on our site that writes files to some of our other servers on the same domain and have been able to do so without issue using file_put_contents like so:
file_put_contents("\\\\server\\folder\\folder\\folder\\".$filename, $file);
Now all of a sudden, to run the same code on our new server I get a Warning on this line, "Failed to open stream: Permission denied". I have permission to access this folder, I can browse to \server\folder\folder\folder and create a file there. I even tried mapping this server to a letter drive on my new web server, and still same error. I can put the file on the local C drive just fine but that's it.
Running fileperms on the folder path gives Warning: fileperms(): stat failed. Running is_writable on the folder path returns false, I just can't see how. Running it on the old Windows 10 web "server" returns true. I've read some things about needing to enable certain settings on the server you're trying to access, but I just can't think of what would allow one VM to access it and not another. Both VM's are logged in with the same user with admin rights. I can bring up the same folder in file explorer and write to it, just not via PHP. What obvious thing am I missing?
Thanks!
After weeks of banging our heads against the wall we finally figured this out. On our old web server, when right-clicking the website from the Sites file tree in IIS, under Manage Website -> Advanced Settings, the Physical Path Credentials field was set to the credentials needed to access these folders. On the new server it was blank. I'm not sure how this got missed but in any case, after entering the correct credentials here everything immediately worked.
I'm trying to transfer files to a shared hosting account (godaddy) and it's failing:
Warning: ssh2_scp_send(): Failure creating remote file: failed to send file in 'local/file/path'
I have SSH2 installed and the connection is working. I even ran a ssh2_exec command to see if I can affect the remote server and I was able to create a directory. I also successfully used scp directly on the command line going both ways... As a further troubleshooting attempt, I reversed the file paths and created a remote server file for it to attempt downloading, I get this error:
failed to open stream: No such file or directory
I called the host to make sure I'm using the correct absolute file path, I've tried many variations, and I still get this error. I think my issue lies somewhere with the remote file path.
The problem code is below:
$localpath=$_SERVER['DOCUMENT_ROOT'].'/local_file_path/' ;
$remotepath = 'website.com/public_html/folder/' ;
$connection = ssh2_connect('website.com', 22);
ssh2_auth_password($connection,'username','password' );
ssh2_scp_send($connection,"$localpath"."SUCCESS.txt", "$remotepath"."SUCCESS.txt", 0644);
I am also having issues trying to use sftp via PHP. I read elsewhere on the web that you have to use double quotes on the file paths, whenever they fail, the printed path appears correct, but perhaps I'm missing something?
UPDATE
I logged into the shared hosting account via sftp with filezilla and discovered that, when copied, the full url path is different then what I was being given when logged in threw regular ftp. I can now upload a file, but, I can't seem to download just yet, ironically... I still get the error about not being able to locate the file path on the remote server.
ssh2_scp_send need absolute path, I solved my issue by using pwd where I need to send my files. ~/ dont works either, it's not an absolute path.
The bottom comment on this page http://www.php.net/manual/en/function.ssh2-scp-send.php refers to a problem you might be having.
I suggest that, use phpseclib or ssh2_sftp
Interesting topics:
Uploading files with SFTP & phpseclib interesting debate with good informations.
How to SFTP with PHP?
If I have a PHP-driven CMS on C:\wamp\www\cms (alias http://localhost/cms) and I want to write log.txt to say Z:\MySite\logs, is that possible using PHP? file_put_contents()? What if Z:\ is a network drive?
What I've tried
file_put_contents("Z:/MySite/logs/log.txt",$log);
And I got:
Warning: file_put_contents(Z:/Mysite/logs/log.txt): failed to open stream: No such file or directory in C:\wamp\www\cms\compiler.php on line 96
edit:
Z:/MySite/logs directory does exist and I have r/w access, though it is on a network drive. (My intention is to dev on my local Apache (C:/) installation then output a preview directly to my network (Z:/) drive so my teammates can view it.) The warning I get (above) seems to indicate it is searching for the Z:\MySite[...] directory inside my C:/wamp[...] directory. I'm stuck.
I'm using move_uploaded_file() to upload images to the server, however it gives the usual error of:
Warning: move_uploaded_file(upload/file.png) [function.move-uploaded-file]:
failed to open stream: No such file or directory in
/home/newuser/public_html/model/account.class.php on line 39
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move
'/tmp/phpuLkUgE' to 'upload/file.png' in
/home/newuser/public_html/model/account.class.php on line 39
This is not permission based as I have set the folder to 777 with root access and ls -l displays it correctly as this.
755 /home/newuser/public_html/model
755 /home/newuser/public_html/model/account.class.php
777 /home/newuser/public_html/upload
PHP Line
move_uploaded_file($_FILES["photo"]["tmp_name"], "../upload/file.png");
The problem I think is down to the Owner/Group setting being configured incorrectly .. a while back I had all of my sites as subdomains in one account:
/home/olduser/public_html/subdomains/index.html
I then changed this and created a new user account to manage a separate website easier and just moved the files across ...
/home/olduser/public_html/subdomains
/home/newuser/public_html/index.html
The new folders in /home/newuser are now owned and grouped as newuser newuser but I think php may be running as nobody olduser so this could be causing the issue?
What can I try to fix this?
Permissions to a particular file / directory don't just apply on the directory itself, but on the whole path leading up to it.
Example:
/home/ - needs 'x' permission (execute)
/home/newuser/ - needs 'x' permission
/home/newuser/public_html/ - needs 'x' permission
/home/newuser/public_html/avatar/ - needs 'wx' permission (execute + write)
It was kind of touched on indirectly in Silver89's feedback under Jack's Answer, but not outright stated - so I wanted to provide an answer to what helped me with this issue which had me scratching my head for a long time. ;)
The best approach that I have found for the destination of move_uploaded_file() is to use the full absolute path. It can vary based on whether you are on a Unix\Linux server or Windows server, but this should give you the basic idea.
On my Unix server at work, you cannot use "../anything" but have to use the full absolute internal file path of /var/www/html/uploads/imagename.jpg.
So that is why your last test worked for you, Silver89 - because your server was probably trying to upload the image to http://yourservername.com/upload/file.png instead of http://www.yourservername.com/yoursubfolder/upload/file.png. It probably threw out the "../" part altogether and that folder didn't exist on the server.
You can find out what that full path name is by logging onto the server (terminal/ssh etc.) and issuing the 'pwd' command or by using PHP code and echoing the getcwd() command in a stripped php file in the folder where your images will go.
This site is helpful in figuring this out based on your server using different PHP Server Config Checking Functions - See the table midway down. You can simply echo these out to the screen such as:
echo $_SERVER["SCRIPT_FILENAME"].
This was a tough one for me so I hope this makes it a little easier for the next person to find - even if this is 8 months old. ;)
I am writing a PHP application, and I've just encountered a really wierd error. After a recent move to a new (Ubuntu) server, i started getting fatal errors on a require_once statement. So obviously the file doesn't exist, or the file permissions are wrong, right?
No, as it turns out:
The file does exist
The file is checked out from an SVN repository. When I go into the folder and list the files (ls or ls -l) the file is clearly there and has the correct file size. When I update the file in the repository and update the working copy, the changes are copied to the working copy. The file has permissions 755, so everyone should be able so see and read it. Other files in the same directory are working just fine.
The file also doesn't exist:
PHP exits with a fatal error because the file can't be found. If I use the find-command, the file is not found. If I try to use nano to edit the file, it claims it does not exist. If I start typing the filename and press tab to autocomplete, it can't find the file.
This behaviour has now happened on two separate servers, but it works fine on a third server. All the servers run Ubuntu 10.04.
Does anyone have any idea what is going on?
EDIT:
File name is AdminIpv4RangeAddFormHandler.inc.
Full error message, with file paths obscured:
Warning: require_once(fullpath/AdminIpv4RangeAddFormHandler.inc): failed to open
stream: No such file or directory in fullpath/anotherfile.inc on line 34 Fatal error:
require_once(): Failed opening required 'fullpath/AdminIpv4RangeAddFormHandler.inc'
(include_path='.:/usr/share/php:/usr/share/pear') in fullpath/anotherfile.inc on line
34
The following command produces no output:
find -name AdminIpv4RangeAddFormHandler.inc -ls
A regular ls -li outputs (amongst others) this row:
2233407 -rwxr-xr-x 1 root root 1597 2011-12-13 08:02 AdminIPv4RangeAddFormHandler.inc
Be careful switching OS's, as some are case sensitive, and others aren't.
I use my Mac for development (case insensitive), so it doesn't matter what casing I use for file_exists() or include_once() etc. Then I deployed to Ubuntu 11 (case sensitive), and all my includes stopped working. I suspect you have the same exact problem.
The tricky part for me was, while using git, my version control didn't detect changes in file name case as a modification. I actually had to rename every file with some sort of prefix, commit and deploy, rename them back to what they should be (with consistent capitalization!), and finally commit and deploy again. It was a complete pain.
Moral of the story--code for your production system, not your dev system.
Take a good long look at your filenames:
AdminIpv4RangeAddFormHandler # from PHP
AdminIpv4RangeAddFormHandler # from find command
AdminIPv4RangeAddFormHandler # from ls output
^
You should pick IP or Ip and stick with the decision everywhere.