Closed Thread Icon

Topic awaiting preservation: Transfer file - [php] (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12045" title="Pages that link to Topic awaiting preservation: Transfer file - [php] (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Transfer file - [php] <span class="small">(Page 1 of 1)</span>\

 
u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 02-12-2002 17:48

How can i transfer a file from PC to FTP? Only via PHP. I mean something like Twitch^ made for our sigs: http://www.miscminutiae.com/sig/ .



[This message has been edited by u-neek (edited 02-12-2002).]

evilclown01
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 02-12-2002 18:18

Is this what you need?
http://www.php.net/manual/en/ref.ftp.php

evilclown01

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-12-2002 19:53

Actually, FTP is not used for this...

Files are uploaded via standard POST method, and PHP has built-in support for handling file uploads: http://www.php.net/manual/en/features.file-upload.php


u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 02-15-2002 22:22

Ok, i get the thing to work, but there is only one problem:

The file exist on the server and i can access the file via ftp, but i cannot view the image in the browser.
When i download the image via ftp and view it, everything is correct.

Did i miss something?

Here is my code:

code:
echo "<form enctype=\"multipart/form-data\" name=\"form\" action=\"admin.php?sect=4\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"1000000\" />\n";
echo "t.jpg: <input size=\"50\" name=\"userfile[]\" type=\"file\" /><br />\n";
echo "s.jpg: <input size=\"50\" name=\"userfile[]\" type=\"file\" /><br />\n";
echo "</form>";



and later in the script:

code:
$RootPath = getcwd();
if (!move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'][0],$RootPath."/snapshot/".$HTTP_POST_FILES['userfile']['name'][0])) {echo "there is something wrong";}
if (!move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'][1],$RootPath."/snapshot/".$HTTP_POST_FILES['userfile']['name'][1])) {echo "there is something wrong";}



Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-15-2002 23:43

Try this. This is something we're using for a content management system that allows us to upload the photos, PDFs, and stuff for related articles. Change $the_path accordingly, and change the two instances of "mydomain.com" to whatever applies.

This will allow you to limit by file type, file size, file dimensions (images), etc, as well as the ability to direct files to directories based on type. It will show a resulting link with file size info.

<?php

$my_max_file_size = "102400"; # in bytes
$image_max_width = "468"; # in pixels
$image_max_height = "300"; # in pixels

$the_path = "/full/server/path/to/upload/dir";
$url_path = "http://www.mydomain.com/up/";

$registered_types = array(
"application/pdf" => ".pdf",
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"text/plain" => ".html, .php, .txt, .inc (etc)",
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"application/x-shockwave-flash" => ".swf",
"application/msword" => ".doc",
"application/vnd.ms-excel" => ".xls",
"application/octet-stream" => ".exe, .fla (etc)"
); # these are only a few examples, you can add as many as you like

$allowed_types = array("image/gif","image/pjpeg","image/jpeg","application/pdf","application/msword");

function form($error=false) {

global $PHP_SELF,$my_max_file_size;

if ($error) print $error . "<br /><br />";

print "\n<form enctype=\"multipart/form-data\" action=\"" . $PHP_SELF . "\" method=\"post\">";
print "\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\" />";
print "\n<input type=\"hidden\" name=\"task\" value=\"upload\" />";
print "\nUpload a file";
print "\n<br />NOTE: Max file size is " . ($my_max_file_size / 1024) . "KB";
print "\n<br /><input name=\"the_file\" type=\"file\" size=\"50\" /><br />";
print "\n<input type=\"submit\" value=\"Upload\" />";
print "\n</form>";

} # END form

if (!ereg("^4",phpversion())) {
function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
for ($i=0; $i < count($haystack); $i++) {
if ($haystack[$i] == $needle) {
return true;
}
}
}
}

function validate_upload($the_file) {

global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;

$start_error = "\n<b>Error:</b>\n<ul>";

if ($the_file == "none") { # do we even have a file?

$error .= "\n<li>You did not upload anything!</li>";

} else { # check if we are allowed to upload this file_type

if (!in_array($the_file_type,$allowed_types)) {
$error .= "\n<li>The file type that you uploaded (" . $the_file_type . ") was of a type that is not allowed, you are only allowed to upload files of the type:\n<ul>";
while ($type = current($allowed_types)) {
$error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
next($allowed_types);
}
$error .= "\n</ul>";
}

if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {

$size = GetImageSize($the_file);
list($foo,$width,$bar,$height) = explode("\"",$size[3]);

if ($width > $image_max_width) {
$error .= "\n<li>Your image is $width pixels wide, but should be no wider than " . $image_max_width . " Pixels</li>";
}

if ($height > $image_max_height) {
$error .= "\n<li>Your image is $height pixels high, but should be no higher than " . $image_max_height . " Pixels</li>";
}

}

if ($error) {
$error = $start_error . $error . "\n</ul>";
return $error;
} else {
return false;
}
}
} # END validate_upload

function list_files() {

global $the_path, $url_path;

$handle = dir($the_path);
print "\n<b>Uploaded files:</b><br />";
while ($file = $handle->read()) {
if (($file != ".") && ($file != "..")) {
$upload_file_size = round(filesize("$the_path/$file")/1024);
print "\n <a href=\"$url_path" . $file . "\">" . $file . "</a> ($upload_file_size kb)<br />";
}
}
print "<hr>";
}

function upload($the_file) {

global $the_path,$the_file_name,$DOCUMENT_ROOT;

$error = validate_upload($the_file);
if ($error) {
form($error);
} else { # cool, we can continue
if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
form("\n<b>Something barfed, check the path to and the permissions for the upload directory</b>");
} else {
print("<b>Filename:</b> $the_file_name<br />\n");
// print("New name: $new_name<br />\n");
$type = "";
$type = (substr("$the_file_name", -3));
if ($type == "gif" &#0124; &#0124; $type == "jpg"){
$type = "photos";
}
print("<b>Type:</b> $type<br />\n");
print("<b>Moving:</b> $the_path/$the_file_name to $DOCUMENT_ROOT/$type/$the_file_name<br />\n");
rename ("$the_path/$the_file_name", "$DOCUMENT_ROOT/$type/$the_file_name");
// list_files();
$upload_file_size = round(filesize("$the_path/$file")/1024);
print("<b>Link:</b> <a href=\"http://www.mydomain.com/$type/$the_file_name\">$the_file_name</a> ($upload_file_size kb)\n");
form();
}
}
} # END upload

print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
print "<html>\n<head>\n<title>Upload example</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\" />\n</head>\n<body>";
switch($task) {
case 'upload':
upload($the_file);
break;
default:
form();
}

print "\n</body>\n</html>";
?>

[This message has been edited by Pugzly (edited 02-16-2002).]

u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 02-17-2002 02:46

I looked at your code, and tried to modify my, but it still didn't work.

BTW: i get a 403 error.

I think i have to change the setting of the file, that every user can access it.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-17-2002 05:48

Run phpinfo and see what the open_basedir setting is. I verified the script works fine if you change these lines
$the_path = "/full/server/path/to/upload/dir";
$url_path = "http://www.mydomain.com/up/";
print("<b>Link:</b> <a href=\"http://www.mydomain.com/$type/$the_file_name\">$the_file_name</a> ($upload_file_size kb)\n");

Change them to the correct settings. It works fine on PHP 4.0 and above, as long as the open_basedir setting isn't too restrictive.

u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 02-20-2002 16:11

Thanks for the script, i fixed it.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-20-2002 18:34

And I'm always looking for ways to optimize/enhance this. So if anyone has suggestions....

u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 02-20-2002 21:42

Well, don't use global variables. Here is a good article about that: http://zend.com/zend/art/art-oertli.php

Ohh, i filtered out the lines i need:
copy($the_file, $the_path . "/" . $the_file_name));
rename ("$the_path/$the_file_name", "$DOCUMENT_ROOT/$type/$the_file_name");

Why doesn't work the code i mentioned above?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-21-2002 01:12

not really relevant in this context.
The global scope issues mentioned in that article mainly refers to security issues related toisession variables and secure areas of sites. globals are still useful in this method.

pugz the main thing I would do here is try to wrap this up into a class for portability.

The other thing is just an ease of coding tip. When printing out a bunch of HTML (in particular with few echoed variables) it's easier to temporarily dump out of PHP parsing mode (even in the middle of a function)

code:
function form($error=false) {

global $PHP_SELF,$my_max_file_size;

if ($error) print $error . "<br /><br />";

?>
<form enctype="multipart/form-data" action="<?= $PHP_SELF ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?= $my_max_file_size ?>" />
<input type="hidden" name="task" value="upload" />
Upload a file
<br />NOTE: Max file size is <?= ($my_max_file_size / 1024) ?>KB
<br /><input name="the_file" type="file" size="50" /><br />
<input type="submit" value="Upload" />
</form>
<?
} # END form




This will work fine.
I'll muck with it a bit see if I can make it a class.



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

[This message has been edited by bitdamaged (edited 02-21-2002).]

[This message has been edited by bitdamaged (edited 02-21-2002).]

mjv
Bipolar (III) Inmate

From: Perth, Australia
Insane since: Aug 2000

posted posted 02-21-2002 06:28

Pugz: i tried to use your script, and i get the following message:
"Parse error: parse error in /home/jamesowe/public_html/sigs/index.php on line 130"

Line 130 is:
if ($type == "gif"

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-21-2002 07:52

Remove space between &#124; &#124; (it should be written like this &#124;&#124; )...


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-21-2002 17:36

A little more on how this script works....

I'm using it for a CMS setup. The site has a different folder for each type of file. So, .jpg and .gif files are considered "photos", and go into /photos. .doc files go to /doc, and .pdf files go to /pdf. That's why I'm checking the file types and piping the type to the COPY part.

It works pretty well. I'll probably start cleaning it up and see if I can't get the same results in less lines. I'm sure Mr. Max could do this same thing in two lines of code....

Dark Phoenix
Paranoid (IV) Inmate

From: Harrow, Ontario, Canada
Insane since: Feb 2002

posted posted 02-21-2002 18:20

copy...(shudders)

"No one's going to give you a map; you've got to walk your own path." - Hot Ice Hilda, Outlaw Star.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-21-2002 19:46

Instead of making comments like that, why don't you actually CONTRIBUTE something.

That's what these forums are for.

Dark Phoenix
Paranoid (IV) Inmate

From: Harrow, Ontario, Canada
Insane since: Feb 2002

posted posted 02-22-2002 01:50

Okay, fine:

copy() is the devil. Use the FTP functions instead.

Why? Most servers have copy() disabled; simply because it's part of the UNIX control set; which includes exec() and a bunch of other functions.

And most of those are disabled to prevent people from mucking around with the servers.

The FTP functions are a lot nicer and easier to use; because you can reuse the code any time you want to upload something.

"No one's going to give you a map; you've got to walk your own path." - Hot Ice Hilda, Outlaw Star.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-22-2002 03:26

Well, the copy() command has worked quite well in our CMS system, hence the posting of the code.

Perhaps you'd like to post a script that does everything that the above script does, only using FTP?

Dark Phoenix
Paranoid (IV) Inmate

From: Harrow, Ontario, Canada
Insane since: Feb 2002

posted posted 02-22-2002 05:36

Very well:

Put this up at the top somewhere:

$conn_id = ftp_connect(/*Your FTP server*/);
$login_result = ftp_login($conn_id,/*Your FTP username*/,/*Your FTP password*/);

And put this were you want the transfer to occur:

if((!$conn_id) &#0124; &#0124; (!$login_result))
{
echo "FTP connection has failed!";
}else{
/*You can put this line in place of copy()*/
$upload = ftp_put($conn_id,/*New File Name*/,/*Pointer To Old File*/,FTP_BINARY);
}

And this at the bottom:

ftp_quit($conn_id);

"No one's going to give you a map; you've got to walk your own path." - Hot Ice Hilda, Outlaw Star.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 02-22-2002 12:29

Hmm, I've just tried this script out at my Dreamhost accont, and I keep getting the "something barfed" message, I seem to remember a notice saying that certain rsiky functions might have been disabled at dreasmhost, could copy() be amongst them? I'll go look now, and if this is the case, try the FTP script offered here, let you know how it works out!

Your pal, -doc-

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 02-22-2002 13:10

Hmm, seems like maybe a different problem, phpinfo() tells me this...

Disabled functions: dl,exec,system,passthru,chgrp,chown,touch,symlink,link

I've got all of my paths right, and the directory is 777 (something that makes me just a wee bit uncomfortable, heh), damn thing should be working now! Hrmph.

Hmm, what user is PHP supposed to be running as? I seem to remember that Dreamhost said it would run as me instead of "nobody", but reading phpinfo() it seems to be HTTP_ENV_VARS["USER"] = root - not sure if *that* is too safe, heh.

Your pal, -doc-

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 02-22-2002 13:24

OK, just for any other Dreamhost users here, Dreamhost is now running an additional PHP processor called "PHP-CGI", which allows you to run PHP with the user as *you* instead of as nobody. You need to alter the extension of your PHP files to ".pcgi", but they then work just as you'd expect them too. (The only difference is the uploaded files now belong to me, and I can do what I want to them using FTP or telnet, cool.)

Your pal, -doc-

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-22-2002 16:48

Interesting....the biggest problems I've run into while working on this is the open_basedir problem. That's quickly resolved with an edit of the .conf file....

Obviously, I could make this much more feature packed, with CHOWNing files to your user name, etc....

Interesting...(heads back to the laboratory to hack some more)

Dark Phoenix
Paranoid (IV) Inmate

From: Harrow, Ontario, Canada
Insane since: Feb 2002

posted posted 02-22-2002 20:06

The first upload script I wrote, I used copy().

And every time I tried to upload something, I got a "File Not Found" error. I checked the CHMOD repeatedly, used echos to see what was being produced, etc. And there was absolutely nothing wrong.

Replacing that with the FTP functions caused it to work 100%.

"No one's going to give you a map; you've got to walk your own path." - Hot Ice Hilda, Outlaw Star.

tr909
Obsessive-Compulsive (I) Inmate

From:
Insane since: Mar 2002

posted posted 03-15-2002 03:10

LS,

Sorry to jump in right here in the middle (just registered) but i had to say this...

I usually have a idea of what i want and roam the php.net/manuals for the exact functions. Like http://www.php.net/move_uploaded_file function (i found it overwrites files, so you could do a if(file_exists) before that... also i found that you (might) have to check filenames. Mac (and Windows?) allow certain characters in filenames which *nixes might not like (*,/,

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-15-2002 05:37

tr909 -

this is actually a bastardized code. I've been cleaning it up and doing some other stuff with it. I *think* we are going to use it as part of the CMS for the new GurusNetwork site. In the posted example, I use the extensions to determine where I need to store the file, as well as making sure I'm only uploading what I should be uploading (it's part of a CMS system, and it has some "idiot checks" built in).

I'll get a cleaner version up soon.

mas
Paranoid (IV) Inmate

From: the space between us
Insane since: Sep 2002

posted posted 11-26-2003 19:43

sorry for being a bit late, but i also tried the same thing doc did.
i have problems with running that on dreamhost, and to be honest...i am a n00b in PHP and i don't exactly understand what to do.
first of all....can anyone help me with the path and the url? whats the difference there? i entered http://www.thespacebetweenus.com/test in the URLpath and well....what do i have to enter in the first one where it says "the full server url" ? ain't that the same? sorry for that stupid question

JKMabry
Maniac (V) Inmate

From: out of a sleepy funk
Insane since: Aug 2000

posted posted 11-26-2003 20:36

your fullpath (on DH) would be something like:

/home/username/thespacebetweenus.com/test/

replace username with yours of course

Jason

mas
Paranoid (IV) Inmate

From: the space between us
Insane since: Sep 2002

posted posted 11-26-2003 21:24

i love you

JKMabry
Maniac (V) Inmate

From: out of a sleepy funk
Insane since: Aug 2000

posted posted 11-26-2003 23:43

give us kisses

Jason

« BackwardsOnwards »

Show Forum Drop Down Menu