How to url fopen local file? Because this is not working on my hosting
fopen("saver.php", "r");
It just opens the file for read, not for execute.
I need this to start parallel process. PHP threads and pecl don't work on the hosting. Options are curl and fopen. But curl is waiting for a response.
Using fopen("http://myserver.com/saver.php", "r"); is not good for me. Because this request is quite slow, for starting parallel process. I want just local fopen, without sending request to outside and than back to hosting.
If you want to execute this you need use system not open.
http://php.net/manual/en/function.system.php
Using a local name of the local machine should work, doesn't it?
fopen("http://localhost/saver.php", "r");
Or maybe even
require('saver.php')
Related
I'm trying to develop an online management system for a very large FLAC music library for a radio station. It's got a beefy server and not many users, so I want to be able to offer a file download service where PHP transcodes the FLAC files into MP3/WAV depending on what the endpoint wants.
This works fine:
if($filetype == "wav") {
header("Content-Length: ". $bitrate * $audio->get_length());
$command = "flac -c -d ".$audio->get_filename().".flac";
}
ob_end_flush();
$handle = popen($command, "r");
while($read = fread($handle, 8192)) echo $read;
pclose($handle);
and allows the server to start sending the file to the user before the transcoding (well, decoding in this case) completes, for maximum speed.
However, the problem I'm getting is that while this script is executing, I can't get Apache to handle any other requests on the entire domain. It'll still work fine on other VirtualHosts on the same machine, but nobody can load any pages on this website while one person happens to be downloading a file.
I've also tried implementing the same thing using proc_open with no difference, and have played with the Apache settings for number of workers and the like.
Is the only way to stop this behaviour to use something like exec and waiting for the encoding process to finish before I start sending the user the file? Because that seems sub-optimal! :(
UPDATE: it seems that other people can still access the website, but not me - i.e. it's somehow related to sessions. This confuses me even more!
Use session_write_close() at some point before you start streaming... You may also want to stream_set_blocking(false) on the read pipe.
I am trying to append files on ftp server using
file_put_contents("ftp://".$ftp_user_name.":".$ftp_user_pass."#".$ftp_server."/".$destFile, $outputStr, FILE_APPEND)
This works fine, but it takes a lot of time to generate a timeout on failure. I want to set the time out for appending the file on FTP. I had a look at stream_context_create() which does support FTP protocol but could not found option for connection timeout, like it has for HTTP protocol. What could be the other way for setting time out for file_put_contents or file_get_contents
Maybe this will help?
ini_set("default_socket_timeout", $seconds);
I am currently opening and writing a text file into my local server with the following:
$mypath="sms_file\\cbsms_";
$fp = fopen($file_name.'.txt', "w");
fwrite($fp, $value. "\r\n");
fclose($fp);
I want to now copy that file to a remote server like /home/project on 10.10.18.23 (home network)
Assuming that I have R/W access in that directory, what would be the best way of achieving this?
The remote server needs to know that there is a request coming in to store a file on it. There are several possibilities here, the easiest would be to run a FTP server.
Another option would be to use the exec() function call scp on the command line (provided you have exchanged ssh keys with the remote server).
Another option would be to create a PHP page on the remote server that accepts POST requests with files and stores them. You must provide your own security measures in this case.
If you can mount the remote host as a permanent volume (via NFS or CIFS), you can use the regular PHP copy() function.
You can try using exec() to run an SCP command:
exec('scp /path/to/file.txt user#homenetworkhost:/home/project/file.txt');
// Obviously, you'll have to set up your SSH permissions and for 'user#homenetworkhost' you'll want to change it to your home network's user and host names.
By the looks of your exmample, I would say your PHP server is on Windows (looking at the backslash in $mypath="sms_file\\cbsms_";), and your remote host is UNIX/LINUX (looking at forward slashes and location /home/project). I would suggest setting up SSH or FTP on the remote host and rather use those protocols than copying it to a network location. Your PHP server (Windows box) will then have to communicate via SSH/FTP and copy the file.
References:
http://php.net/manual/en/book.ftp.php
http://php.net/manual/en/function.ssh2-scp-send.php
I'm having a problem with PHP where i'm writing to a file very often, and sometimes it takes a long time to open that file.
The complete description is here:
fopen file locking in PHP (reader/writer type of situation)
My question is how can I get fopen to timeout in, say, 50ms.
I looked at stream-context-create but that seems to be for HTTP, or at least, if it'll work for local files, I'm not sure how to specify the option in the array.
Any ideas?
Thank you!
Daniel
I'm not sure what you're trying here, but in some platforms (not Windows, though), you can open a file in non-blocking mode with the n flag:
$f = fopen("/tmp/foo/bar", "wn+");
This should return immediately. You can then probably use stream_select with a timeout of 50 ms.
I say "probably" because this flag is not documented.
changing the default_socket_timeout variable in the php.ini to '1', would that helps?
any idea why fopen would timeout for a file if it is on my server and I know the url is correct?
update: sorry, i should have mentioned this is in php.
the code is:
fopen($url, 'r');
It works if i put in a relative path for the file, but not if $url is a url in my server (but it works for google.com). Thanks for the help.
Alaitnik's answer was right. The problem only appears when i access my own server files through the ethernet interface. How can I fix this? I need to be able to access the file from the ethernet interface because the url loads dynamically (it's generated from a wordpress cms, so the url doesn't technically exist as a file on my server)
you can use
ini_set('default_socket_timeout',2);
before opening the fopen $url . This actually set the default socket connection timout without responding.
Stream_set_timeout sets time out on the stream that is established via fopn or socket opening functions.
Try this may be helpful for you.
It appears that you're trying to download a file from your own server using the HTTP protocol from a program running on that same server?
If so, the timeout problem is likely to be web server or network configuration related. Timeouts normally only happen because either:
the server really is taking a long time to send back the answer, or
the TCP connection is being blocked
For example, it may be that your local firewall rules only permit access to www.example.com if those queries come from the ethernet interface, but a locally made connection would try to go via the loopback interface.
maybe your "allow_url_fopen" is set to "Off"
check your php.ini file or phpinfo()
If you are trying to get the HTML of a URL, I suggest using curl instead of fopen.
fopen is best used with local files, coz it does not "know" how to deal with the idiosyncrasies of a network resource.
Check the comments on the documentation of fopen. There's a whole lot of gold in there.
Took me ages to solve this, but here I found it, thanks to Alnitak. Opening the file with localhost in the URL instead of the hostname was what did the trick for me.