View Full Version : PHP upload question
alexjewell
10-10-2006, 11:08 PM
My girlfriend doesn't really like ftp clients, and I don't want to give her full control over the files NOT in her folder. So, I've devised a plan: I'm going to use the php ftp functions so she can just go to the page and upload, and it will go into her folder.
I wrote out a script, but I just have a few questions.
First off, here's the code:
include('inc.php');
$connect = ftp_connect($address) or die("Cannot connect to server");
$login_result = ftp_login($connect, $userid, $passwd) or die("Cannot login to server");
$file = $_REQUEST['file'];
ftp_put($connect, $file, $file, FTP_ASCII);
ftp_close($connect);
Now, $address, $userid, and $passwd are in the inc.php file, and I think they're pretty self explanatory. index.php has the upload form on it, and it submits to this page. Now, first of all, how do we determine whether or not the file should be ASCII or BINARY? She'll be mostly uploading image files, so BINARY may be the way to go, but what if she uploads a .txt file or something? Then, somehow, we need to recognize this and switch it to ASCII. Is there a way to check that before uploading it?
Also, how do we specify the folder on the server to upload to? Or is it that this file is in that directory and so it knows to just put the file in that directory?
I haven't tested this code out yet, I wanted to get feedback first: will it even work in the first place?
I also plan on displaying the files in the directory for her, by doing this:
$directory_name = ftp_pwd($connect);
$filesArr = ftp_nlist($connect, $directory_name);
foreach ( $filesArr as $value) { echo $value."\n"; }
Will that work?
And then one last thing:
What if a file is uploaded with the same name as a file already in the directory? Is there a way to detect this and add a "2", "3", "4", etc on the end of the filename to prevent errors? Or, even throw back a message saying "Hey, there's already a file with this name?"
djr33
10-11-2006, 01:27 AM
I don't want to give her full control over the files NOT in her folder.Did you tell her about this? Ha :p
Looks pretty good to me. I haven't used the ftp php functions, so not sure, though. Test it out :)
As for overriding, i'm sure there's a default way to check. If by chance there isn't, just compare it to the existing files before replacing. just check the size of the file by that name, for example. If an error (use @ to not display the error), then upload the file. If not, do a confirm, or change the name of one, or, just replace the old file...
Why are you using FTP for this? Surely, since the file has already been moved to a temporary directory, it's easier to simply use move_uploaded_file (php.net/move-uploaded-file)() and then chmod/chown it if necessary?
alexjewell
10-11-2006, 01:23 PM
Twey, the file isn't already on the server. We're uploading it from HER computer to the server, into her folder. She just needs to upload images and maybe some misc text files, etc. My two questions were basically one, how do we determine ASCII or BINARY? Is there a way to find the file extension? I may just have her select ASCII or BINARY along with the form, but I'd have to explain the difference to her.
My other question was what if the file already exists? Djr, what exactly would that code look like? How do I check if that file already exists?
nelis
10-11-2006, 04:56 PM
Well to check if the file exists you use this:
if(file_exists("$filename"))
For the extension you could use substr... example
$extension = substr($filename,-1,3);
That's assuming the extension is 3 chars
Twey, the file isn't already on the server.But if you're handling uploads with PHP, it is. The PHP script isn't run until the file has been accepted and saved to a temporary directory.
alexjewell
10-11-2006, 08:32 PM
Why do these functions exist, then?
Give me an example of how to do this with move_uploaded_file()?
$basedir = '/home/girlfriend/';
move_uploaded_file($_FILES['file']['tmp_name'], $basedir . $_FILES['file']['name']);
blm126
10-12-2006, 12:52 AM
Why do these functions exist, then?
To transfer files to another server.
djr33
10-12-2006, 02:25 AM
Right. FTP is for remote servers. There's no point in using it on your server if you've already got access :)
nelis
10-12-2006, 08:30 PM
I agree with djr33...
I think the point of the ftp functions is to get to a third party server.. Let's say for instance you want to upload a file to your web page, but you're really storing on another server (that has ftp access)...
alexjewell
10-13-2006, 12:20 AM
How do you figure out 'tmp_name'?
alexjewell
10-13-2006, 12:31 AM
Ok, I'm getting an error with this code - undefined index?
$file = $_REQUEST['file'];
$basedir = 'd:\webspace\ADMIN\alexjewell.com\WWW\amanda';
$tmp_name = $_FILES[$file]['tmp_name'];
$new_name = $_FILES[$file]['name'];
move_uploaded_file($tmp_name , $basedir.$new_name);
echo "<p>File uploaded successfully</p>";
I think it's basically saying it can't find where the temp file's been uploaded. How do we find this out?
ps - should I be using something along the lines of is_uploaded_file or $HTTP_POST_FILES?
alexjewell
10-13-2006, 11:59 AM
Ok, I've worked around some code and done some things. The PHP errors are gone, but now my second die statement is showing up: "Couldn't upload file".
I decided to echo the address of the new file, and it's really weird:
I practiced with a file called hovers.js...this is what it echoed: d:\webspace\ADMIN\alexjewell.com\WWW\amanda\h
It took the first letter and dropped the rest? I then used a file called js.php, and it ended up just like the h: amanda\j.
$file = $_REQUEST['file'] or die("<p>Could not retrieve file</p>");
$basedir = 'd:\webspace\ADMIN\alexjewell.com\WWW\amanda\\';
$tmp_name = $file['tmp_name'];
$new_name = $file['name'];
echo "<p>".$basedir.$new_name."</p>";
move_uploaded_file($tmp_name , $basedir.$new_name) or die("<p>Couldn't upload file</p>");
echo "<p>File uploaded successfully</p>";
What's going on?!
alexjewell
10-13-2006, 12:53 PM
Ok, the problem has been solved. I went with a different avenue altogether. But if anyone would like to point out the problems with the above script, that'd be cool.
ajitsingh
10-13-2006, 01:03 PM
First you can find out that it uses ASCII or binary.
Then put it.
ajitsingh
10-13-2006, 01:32 PM
<?php
// --------------------------------------------------------------------
// THE TRIGGER
// --------------------------------------------------------------------
// set the various variables
$ftproot = "/public_html/test/";
$srcroot = "/home/kristy/scripts/";
$srcrela = "iwm/";
// connect to the destination FTP & enter appropriate directories both locally and remotely
$ftpc = ftp_connect("ftp.mydomain.com");
$ftpr = ftp_login($ftpc,"username","password");
if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }
// start ftp'ing over the directory recursively
ftpRec ($srcrela);
// close the FTP connection
ftp_close($ftpc);
// --------------------------------------------------------------------
// THE ACTUAL FUNCTION
// --------------------------------------------------------------------
function ftpRec ($srcrela)
{
global $srcroot;
global $ftproot;
global $ftpc;
global $ftpr;
// enter the local directory to be recursed through
chdir($srcroot.$srcrela);
// check if the directory exists & change to it on the destination
if (!ftp_chdir($ftpc,$ftproot.$srcrela))
{
// remote directory doesn't exist so create & enter it
ftp_mkdir ($ftpc,$ftproot.$srcrela);
ftp_chdir ($ftpc,$ftproot.$srcrela);
}
if ($handle = opendir("."))
{
while (false !== ($fil = readdir($handle)))
{
if ($fil != "." && $fil != "..")
{
// check if it's a file or directory
if (!is_dir($fil))
{
// it's a file so upload it
ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
}
else
{
// it's a directory so recurse through it
if ($fil == "templates")
{
// I want the script to ignore any directories named "templates"
// and therefore, not recurse through them and upload their contents
}
else
{
ftpRec ($srcrela.$fil."/");
chdir ("../");
}
}
}
}
closedir($handle);
}
}
?>
natatkinson
06-05-2008, 02:55 AM
Ok, the problem has been solved. I went with a different avenue altogether. But if anyone would like to point out the problems with the above script, that'd be cool.
I'm curious how you solved this problem, I am working on trying to do the same thing right now, upload a file to a different server. I would cry if you could help me.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.