i have GoDaddy shared webspace with FTP access that contains a folder with images. These images are changing every day.
Im looking for advice on what i need to do to get these images onto my server in the workplace, maybe every hour or so. The server doesnt have IIS installed is there any other way to do this?
Am i able to do a PHP script that can put all the images onto the server using the ip or something?
I had the same issue as I had two images on a remote server which I needed to copy to my local server at a predefined time each day and this is the code I was able to come up with...
try {
if(#copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) {
} else {
$errors = error_get_last();
throw new Exception($errors['type'] . " - " . $errors['message']);
};
} catch(Exception $e) {
//An Error Occurred Downloading The Requested Image
//Place exception handling code here
};
I then created a cron job which ran this file on a daily basis but you would be able to run is as frequently as you needed to based on how frequently the source image changes on the source server.
A few points to explain the code...
As the code is run in the background I use the # symbol to silence visual references to any errors which occur with the copy command as they won't be viewable anyway. Instead if copy results in an error it returns false and triggers throwing a new exception which takes the error type and error message and throws a new exception for it which can then be handled in the catch block and can be actioned however you need such as making a local log entry, sending an error email, whatever you need.
As the second paramater in the copy command you will see that the path results in the filename being named based on the current date. This name can be anything but needs to be unique in the destination for it to work. If you plan on overwritting the image each time the copy is done then you can statically code the name as the same name but if you want to maintain a history then you would need to come up with a file naming solution on your local server to ensure that the files don't get overwritten each time the cron runs.
The first parameter of the copy statement has been written on the assumption that the source image file is named the same each time, if the name changes the you will need to identify how the naming is achieved and code a variable to build that name and insert it as the source filename.
This code does not alter the format of the source image file so to ensure no corruption occurs and the image can still be shown after the copy you need to ensure that the source image file and the local copy of the image file have the same file extensions, so if the source image file is a .gif file then you need to make sure the file extension in the second copy parameter is also set to .gif
I'll try to answer here instead of continuing the comment-spam ;)
you got your webspace with FTP access. let's just call it webspace;
then you got your server at your workplace. let's just call it workplace;
after all you need one server (also can be webspace for example) where you are able to run PHP. let's call it php-server;
step 1
at the workplace setup a FTP server, for example FileZilla. you setup your FTP server so that you can connect to it. so make an account and set it up so you can access the folder(s) where you want to save your images. also make sure you got access from outside your workplace - firewall settings etc.
step 2
if you can run your PHP scripts on your webspace this would be the easiest way. you can directly access the image files, establish a connection to your FTP server at workplace and upload the files.
if the PHP server is somewhere else, you have to establish a connection from the php-server to your webspace; download your files to your php-server; upload your files to the FTP server at your workplace.
will be a bit of work as you can see, but should be possible to do.
Create a .bat file that uses the ftp command line functions to get the data you want from the server. Save that .bat file somewhere and create a Scheduled Task to run the script every hour.
You can even store the actual sequence of ftp commands in a separate file (e.g. ftpcmd.dat) and call them from the script
ftp -n -s:ftpcmd.dat SERVERNAME.COM
Example ftp command file:
user MyUserName
Password
bin
cd \your\path\images
mget * C:\Temp\
quit
Related
I want to know whether there is anyway to get the client side absolute path of selected file!
$_FILES['xml_file']['tmp_name']; This also provides path of selected file but its server side path... Its result is something like this C:\xampp\tmp\php2679.tmp
I want C:\Users\Sami\Desktop\data\myfile.xml where my actual file is placed.
Because I want this actual path to use again for fetching and saving etc automatically (by code).
you may try echo realpath($_FILES['xml_file']['name']); on your php.
then the output will be:
C:\Users\Sami\Desktop\data\myfile.xml
When you click the browse button it chooses the file. When you click the submit button, the entire file is being sent from the browser to the web server, and PHP is indeed putting the entire file in the php servers temp directory. Then, your script gets called.
So when you try to get absolute path from $_FILES['xml_file']['name'] or $_FILES['xml_file']['tmp_name'] or realpath($_FILES['xml_file']['name']) you will get the path on server.
The solution to this problem would be to simply copy the file somewhere, and not ask for the file upload again. You'll need to be sure that where ever you copy it to on your web server, that the server has permissions to do so. Usually that involves giving write permissions for the user which the web server runs as (usually user 'apache' or 'nobody' for apache based servers) to a special directory you've created for the purpose.
Copy is available in PHP. http://us3.php.net/manual/en/function.copy.php
Your only trick will be what to name it so you can find it again for the second request, and deciding what directory to put it into.
I have a site for media conversion where users can upload video, audio or image files and it is converted in to 3 popular formats. Conversion script works fine but I am having some issues with the tmp directory where the files get uploaded to. I have tried 2 scenarios I am fine with either but neither works and it seems to me to be permissions related but I can seem to fix the problem.
I am working locally right now while I work out the kinks.
Scenario 1:
File is uploaded to default local tmp directory (C:\WINDOWS\tmp) - works fine
Attempt to run conversion script using tmp file that was uploaded and it doesn't work - run from command line works perfectly fine though
Scenario 2:
File is uploaded to directory I have given IIS_IUSRS full control of (for testing) and file won't upload - yes the directory is spelt correctly (I changed the upload_tmp_dir value in php.ini)
Both the site the javascript that send the XMLHttpRequest to the PHP file, as well as the site the PHP file itself reside on are IIS sites so I assume the script is being run as IIS_IUSRS.
EDIT: Temp file is no longer being created at all for Scenario 1, can't figure out why I am assuming playing with permission messed something up because the code hasn't changed. I've given Modify to IIS_USRS and USERS to try and get it working again but no luck :( although the error log is still writing to the same folder...weird
NOTE: The "tmp_name" value of the $_FILES variable I am sending still has a value of "C:\WINDOWS\Temp\'filename'" but the file is not there
EDIT: Another new development, it appears it is NOT a permissions issue because I can create a temp file via $temp_file = tempnam(sys_get_temp_dir(), 'Test'); however it obviously does not contain the uploaded data so it does not solve my problem
PHP is ignoring the upload_tmp_dir because of one setting on APPLICATION POOLS.
It's not php-cgi.exe nor php.ini or a permissions issue.
Go to the application pool of the website experiencing the issue:
1. right click
2. select advanced settings
3. scroll to LOAD USER PROFILE and set it to FALSE.
that did the trick for me.
This is less of a problem solved and more of a workaround. The issue seems to be something with the
$_FILES['file']['tmp_name'];
When I echo the contents it looks as I expect however no file appears. So rather than taking advantage of that process that happens naturally, I have had to take a manual approach. What I have done is the following:
create a temp file
$temp_file = tempnam(ini_get('upload_tmp_dir'), 'php');
then add the content from the temp file created during the $_POST request. (which some how are in the $_FILES variable even though the file is not when I look in the directory)
file_put_contents($temp_file, file_get_contents($_FILES['file']['tmp_name']));
the I have my temporary file for processing.
I'm using the library phpseclib0.3.7 to connect via SFTP to my web server. The users that are present have the following structure:
/dirhome/FirstUser/FirstUser/
/dirhome/SecondUser/SecondUser/
/dirhome/....../.....
Where:
dirhome:
own -> root:root
permission -> 700
FirsUser: (is the name of a user (example))
own -> root:root
permission -> 755
FirsUser: (is the name of a user (example))
own -> FirstUser:mygroup
permission -> 700
The same structure for the second user and so on. With this facility, users can view / edit / create content only within their directory.
I can easily connect in PHP using the library and view folders/files, but do not know how to upload files from user's local PC on the remote server within your personal folder.
The library provides the method:
->put($remotepath, $localpath, NET_SFTP_LOCAL_FILE);
Il problema รจ:
How can I open a dialog box to allow the user to select a file on user's PC and upload in her personal directory?
If I put: $localpath = "C:\\Users\\****\\Desktop\\test.txt"
I get:
C:\\Users\\****\\Desktop\\test.txt is not a valid file
But if I take a file that resides locally on the server works fine, but it does not make sense because users can not put its files. There are several days that I try but without success.
Also the download does not work, essentially for the same problem.
PHP is a server side scripting language. When your browser requests a PHP generated site, PHP runs on the server and your browser only gets to see the result. For that reason, PHP obviously has no way to access client side files: it already ran before anything even reaches the client computer.
I'm assuming the server with the PHP files on it and the SFTP server are different servers, otherwise your whole question doesn't make too much sense.
So, what you need to do here is a two step approach: First, you need to upload the files to the server who runs the PHP files the regular way, using a HTTP POST request. You can send the request to a PHP script, that then uses SFTP to move the files to the other server.
For downloads (as you asked this in your comments) it works similar: the browser requests a PHP script that fetches the file from the SFTP server, and then sends it to the browser as HTTP response. The browser should then display a regular file download dialog and the user can select where to store it.
FOr uploading you should consider using some kind of cron job or a background job started using PHP's exec() instead, as you will most likely either run into max execution timeouts or have to set them way higher than you should if you upload them usign PHP, especially for large files. The alternative is to use a separate PHP configuration (depending on what version of PHP you are running you can use .htaccess files, .user.ini files or different PHP-FPM pools for that) to increase the execution time for only the upload script.
I would be grateful for help concerning this issue:
User clicks on a link:
the link itself has the parameter that tells which file needs to be unzipped to /unzip folder. After a file is unzipped, I would like to open the file.
How can I do this? I have the unzip part coded already.
I can suggest a following solution:
You create a folder on the server where you will unzip the files to.
You create an .htaccess file there which specifies your own php script as 404 Error handler
In your php script you parse URL and identify which file to unzip, unzip it and redirect user to the newly created file
If you need to clean the unizpped files, you can create a cronjob which will remove files older than a certain time
What you get from that is:
File transfer from server to user is handled by web server
You actually cache your work as 404 handler won't run if you have the file in place
You can significantly lower the server load as this approach reduces the amount of operations performed on server side (when file exists)
The description above assumes Apache as a web server
I have a directory on a remote machine in which my clients are uploading (via different tools and protocols, from WebDav to FTP) files. I also have a PHP script that returns the directory structure. Now, the problem is, if a client uploads a large file, and I make a request during the uploading time, the PHP script will return the file even if it's not completely uploaded. Is there a way to check whether a file is completely uploaded using PHP?
Setup your remote server to move uploaded files to another directory, and only query the directory files are moved to for files.
AFAIK, there is no way (at least cross-machine) to tell if a file is still being uploaded, without doing something like:
Query the file's length
Wait a few seconds
Query the file's length
If it's the same, its possibly completed
Most UNIX/Linux/BSD-like operating systems has a command called lsof (lsof stands for "list open files") which outputs a list of all currently open files in the system. You can run that command to see if any process is still working with the file. If not, your upload has finished. In this example, awk is used to filter so only files will show that are open with write or read/write file handlers:
if (shell_exec("lsof | awk '\$4 ~ /.*[uw]/' | grep " . $uploaded_file_name) == '') {
/* No file handles open for this file, so upload is finished. */
}
I'm not very familiar with Windows servers, but this thread might help you to do the same on a Windows machine (if that is what you have): How can I determine whether a specific file is open in Windows?
I would think that some operating systems include a ".part" file when downloading a file, so there may be a way to check for the existence of such a file. Otherwise, I agree with Brian's answer. If you were using the script on the same system it is simple enough to tell using move_uploaded_file()'s return if it was being uploaded by a PHP script, but it does become a challenge pulling from a remote directory that can be added to with different protocols.