PDA

Click to See Complete Forum and Search --> : Upload files with PHP [Resolved*3]


NoteMe
Apr 17th, 2004, 11:29 AM
And here we go again. More or less just me asking questions here now...:D

I found an example in my Beginning PHP4 book from wrox on how to upload files to the server. But the explenation is a bit hard to understand. Here is the code


<?
//file_upload.php
$archive_dir = "./bildebox";
function upload_form() {
global $PHP_SELF;
?>
<FORM METHOD="POST" ENCTYPE="MULTIPART/FORM-DATA"
ACTION="<? echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="upload">
Upload file!
<INPUT TYPE="FILE" NAME="userfile">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="upload">
</FORM>
<?
}

function upload_file() {
global $userfile, $userfile_name, $userfile_size,
$userfile_type, $archive_dir, $WINDIR;

if(isset($WINDIR)) $userfile = str_replace("\\\\","\\", $userfile);

$filename = basename($userfile_name);

if($userfile_size <= 0) die ("$filename is empty.");

if(!@copy($userfile, "$archive_dir/$filename"))
die("Can't copy $userfile_name to $filename.");

if(!isset($WINDIR) && !@unlink($userfile))
die ("Can't delete the file $userfile_name.");

echo "$filename has been successfully uploaded.<BR>";
echo "Filesize: " . number_format($userfile_size) . "<BR>";
echo "Filetype: $userfile_type<BR>";
}
?>
<HTML>
<HEAD><TITLE>FILE UPLOAD</TITLE></HEAD>
<BODY>
<?
if($action == 'upload') upload_file();
else upload_form();
?>
</BODY>
</HTML>


In my "public_html" folder, I have a folder called "klubbny", in that folder I have two folders. One called "oppdater" where I have this page, and one called bildebox, where I want to upload the file to.

What am I doing wrong here. I am getting the message:


Can't copy f.gif to f.gif.


Am my link in the top:

$archive_dir = "./bildebox";

wrong...should I write the whole path, or what. Not getting the path system here completly.

Thanks ии

NoteMe
Apr 17th, 2004, 12:21 PM
And an other thing that concerns me is that I want to use it as vbforums does. Have a lot of other textboxes and stuff to, and not uplad it before it goes to the next page. Is that possible?

visualAd
Apr 17th, 2004, 12:39 PM
Its clear that the copying of the uploaded file is failing. This may be due to the fact that you are using the relative path as your $archive_dir and your PHP script is not executing in the directory you expect your PHP script to be in.

Modify your source so you can get some debugging information:

if(!copy($userfile, "$archive_dir/$filename")) {
echo ('Current directory: ' . getcwd () . '<br />');
die("Can't copy $userfile_name to $filename.");
}

That should cause the script to spit out whatever error message is being generated by the copy function and also print the current working directory of the script.

kows
Apr 17th, 2004, 01:38 PM
eww, register_globals is on?

This line should be making your entire thing not work:
if(isset($WINDIR)) $userfile = str_replace("\\\","\\", $userfile);
If you have warnings/errors turned off, you may not have caught that.

I don't know what you mean by having it "not upload until it goes to the next page".. You can add other form elements to your page, and also have your upload function do any uploading, but only if it's necessary. You don't have to have a form that's just for uploading.

If you're interested, this is what I use for uploading things, and with a few modifications you can have a nice uploader..
<?
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$url_this = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$upload_dir = "upload_files/";
$upload_url = $url_dir . "/" . $upload_dir;
$message ="";

if($_FILES['userfile']){
$message = do_upload($upload_dir, $upload_url);
}else{
$message = "Invalid File Specified.";
}
echo $message;

function do_upload($upload_dir, $upload_url){
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_type = $_FILES['userfile']['type'];
$file_size = $_FILES['userfile']['size'];
$result = $_FILES['userfile']['error'];
$file_url = $upload_url.$file_name;
$file_path = $upload_dir."gay" . $file_name;
if($file_name == "") {
$message = "Error: Invalid filename.";
return $message;
}elseif($file_size > 500000){
$message = "Error: Filesize exceeds 500K.";
return $message;
}elseif($file_type == "text/plain") {
$message = "Error: Unable to upload plain text files." ;
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
$message = ($result) ? "File Location: <a href=$file_url>$file_url</a>" : "There was an error while uploading the file.";
return $message;
}
?>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload Image<input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload File">
</form>

NoteMe
Apr 18th, 2004, 05:18 AM
Originally posted by visualAd
Its clear that the copying of the uploaded file is failing. This may be due to the fact that you are using the relative path as your $archive_dir and your PHP script is not executing in the directory you expect your PHP script to be in.

Modify your source so you can get some debugging information:

if(!copy($userfile, "$archive_dir/$filename")) {
echo ('Current directory: ' . getcwd () . '<br />');
die("Can't copy $userfile_name to $filename.");
}

That should cause the script to spit out whatever error message is being generated by the copy function and also print the current working directory of the script.


Thanks, that gave me some more info. And it looks like the problem is that I am not good enought to find the right directory...:D


I have to go up one level, then down again into an other folder called "bildebox".

I thought I could just write

$archive_dir = "./bildebox";

to go down one level, and then up again. But it now looks like it is trying to find ./bildebox in my current folder. So what do I have to write in front of the /bildebox to make it go up one folder before it goes down to the right one.

NoteMe
Apr 18th, 2004, 05:24 AM
Originally posted by kows
eww, register_globals is on?

This line should be making your entire thing not work:
if(isset($WINDIR)) $userfile = str_replace("\\\","\\", $userfile);
If you have warnings/errors turned off, you may not have caught that.


I can see that vBulletin boards have skiped some \ at this line, when I colpied pasted. It is actualy 4 \ at the first parameter, then 2 in the second one. Not sure what $WINDIR is anyway, but to me it looks like that line is converting from a unix path to a windows path. Not sure what oyu ment that it will make my code fail.


Originally posted by kows

If you're interested, this is what I use for uploading things, and with a few modifications you can have a nice uploader..
<?
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$url_this = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$upload_dir = "upload_files/";
$upload_url = $url_dir . "/" . $upload_dir;
$message ="";

if($_FILES['userfile']){
$message = do_upload($upload_dir, $upload_url);
}else{
$message = "Invalid File Specified.";
}
echo $message;

function do_upload($upload_dir, $upload_url){
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_type = $_FILES['userfile']['type'];
$file_size = $_FILES['userfile']['size'];
$result = $_FILES['userfile']['error'];
$file_url = $upload_url.$file_name;
$file_path = $upload_dir."gay" . $file_name;
if($file_name == "") {
$message = "Error: Invalid filename.";
return $message;
}elseif($file_size > 500000){
$message = "Error: Filesize exceeds 500K.";
return $message;
}elseif($file_type == "text/plain") {
$message = "Error: Unable to upload plain text files." ;
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
$message = ($result) ? "File Location: <a href=$file_url>$file_url</a>" : "There was an error while uploading the file.";
return $message;
}
?>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload Image<input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload File">
</form>

This code looks much better then the one I found. Even if it called my file gay after it was uplaoded...;)....but the problem with going up one folder level before down in an other folder is still the same.

NoteMe
Apr 18th, 2004, 05:27 AM
I have made a picture so you can see more clear what I mean with the folders.


http://vbforums.com/attachment.php?s=&postid=1672797

My PHP file is in the "oppdater" folder. Then I guess I have to write this line:

$archive_dir = "./bildebox";

in a way that it makes it go up to "klubbny" then down in the other folder called "bildebox".

visualAd
Apr 18th, 2004, 05:35 AM
$archive_dir = "../bildebox" :D

One dot . corresponds to the current directory and two dots .. refers to the parent directory.

NoteMe
Apr 18th, 2004, 05:42 AM
Hehe...ok...thanks...but actually I tried that yesterday.

And it didn't work. So there must be an other problem.

I get a permission denied error. But on the folder I have the read/write/execute persmissions, so it can't be that...:(


Warning: copy(../bildebox/f.gif): failed to open stream: Permission denied in /home/klub/public_html/klubbny/oppdater/file_upload.php on line 27
Current directory: /home/klub/public_html/klubbny/oppdater
Can't copy f.gif to f.gif.

NoteMe
Apr 18th, 2004, 05:46 AM
And KOWS code gives me this error:


Warning: move_uploaded_file(../bildebox/gayf.gif): failed to open stream: Permission denied in /home/klub/public_html/klubbny/oppdater/upload3.php on line 35

Warning: move_uploaded_file(): Unable to move '/tmp/php0UudLu' to '../bildebox/gayf.gif' in /home/klub/public_html/klubbny/oppdater/upload3.php on line 35
There was an error while uploading the file.


I am so lost now. This is things that we don't learn at school, but that we should have learned...:(

visualAd
Apr 18th, 2004, 05:52 AM
You need to make that directory world writeable otherwise PHP cannot create the file.

visualAd
Apr 18th, 2004, 05:57 AM
Are you on Windows or Unix???

For some reason I thought you were on Windows :::slaps himself:::

It depends on how ther server has been set up. The error is caused becuase the PHP process does not have permission to create files in that directory.

I think what you'll have to do is either create the directory using PHP, so as it is automatically owned by the httpd/php use and group and then it should work. Or if you can change the permissions on the directory making it world writeable.

NoteMe
Apr 18th, 2004, 05:59 AM
Thanks, then it finaly worked.

I thought that was pretty scary to do. But I guess as long as no other then the admins know about that folder, it can't be miss used. Or what?


f.gif has been successfully uploaded.
Filesize: 4,951
Filetype: image/gif



Will go off and try to make all the other fields work on the same page now. Thanks for all your help, both of you.

ии

NoteMe
Apr 18th, 2004, 06:01 AM
Originally posted by visualAd
Are you on Windows or Unix???

For some reason I thought you were on Windows :::slaps himself:::

It depends on how ther server has been set up. The error is caused becuase the PHP process does not have permission to create files in that directory.

I think what you'll have to do is either create the directory using PHP, so as it is automatically owned by the httpd/php use and group and then it should work. Or if you can change the permissions on the directory making it world writeable.


I made it public writebal, and then it worked. But I guess it is more safe to make PHP create it. So PHP can be in the group, rather then make it writebal for everyone.

Thanks

visualAd
Apr 18th, 2004, 06:03 AM
Originally posted by NoteMe
But I guess as long as no other then the admins know about that folder, it can't be miss used. Or what?
If you've had to make the folder world writeable, it is best to put it outisde the document root so only your PHP script can get to it.

If you are the admin of the server though you can always set it up so that PHP runs under a different user from the web server and chown all your web server files to the user which the PHP script runs under.

NoteMe
Apr 18th, 2004, 06:10 AM
THanks, but I am not the admin at that server. Not sure how they have set up that server. I am just trying to help a friend out here with his page, becuase I soon have my exam in PHP, and I just found out that I suck.....so I had to start somewhere.


But I will figgure out something to make it safe anyway. Thanks for everything.

NoteMe
Apr 18th, 2004, 11:30 AM
OK...last Q about this topic...

I have now one PHP file, that first makes you upload a file, then when you press upload, it uplaods the file, and then the rest of the form are made so you can write text an stuff that later will be stored in a MySQL database.

But in the function where I am uploading the file, I am also making the other text fields and so on. And in one of those I want the name of the file I uploaded. But how do I add the name of the file in that text field. Here is the striped down code for just this:


function upload_file() {
global $userfile, $userfile_name, $userfile_size,
$userfile_type, $archive_dir, $WINDIR;

if(isset($WINDIR)) $userfile = str_replace("\\\\","\\", $userfile);

$filename = basename($userfile_name);

if($userfile_size <= 0) die ("$filename is empty.");

if(!copy($userfile, "$archive_dir/$filename")) {
echo ('Current directory: ' . getcwd () . '<br />');
die("Can't copy $userfile_name to $filename.");
}

if(!isset($WINDIR) && !@unlink($userfile))
die ("Can't delete the file $userfile_name.");

?>

<form method="get" action="ferdig_linkk.php" style="margin:0; padding:0">

<div id="Info">
Bildefilen:
<input type="text" name="lBilde" size="60" value="<? "$filename" ?>"/>

</div>

</form>
<?
}
?>


I have bolded out the line where I am trying to get the file name in the text field using the value property.

NoteMe
Apr 18th, 2004, 11:41 AM
I am sutch a short thinker. An echo did it for me...:D




<input type="text" name="lBilde" size="60" value="<? echo "$filename";?> "/>

kows
Apr 18th, 2004, 01:15 PM
Use a shortcut;
<input type="text" name="lBilde" size="60" value="<?=$filename;?>" />
Also, I was literally laughing out loud when I read that my uploader had called your file gay, but it's because of this line:
$file_path = $upload_dir."gay" . $file_name;
I was having trouble changing a lot of stuff with the original script, so I added that as an error check while changing it and never noticed I still had it there.

NoteMe
Apr 18th, 2004, 01:28 PM
Thanks for the short cut.


And when I saw that gay thingy, I though you thought my skills in PHP was so bad that you just wrote that to give me a hidden message...:D....good to hear you didn't mean to have that extra name to my files...;)


BTW: I am still working on the page, and have been sitting for an hour now with the same "error". First of all I am using:

printf("<tr><td>%s</td><td>%s</td></tr>\n",$Overskrift, $Dato);

To make a small table so the headings get pretty. But I want the right cell to be right aligned and the left one to be left aligned. But I can't manage it. Why can't I just put in a nice align="right on the right <td>???

The other thing is that I am getting both the heading and the rest of the text from MySQL and print it in a "visual" box. I want the heading in one font, and the rest with an other one. But since I want it to apear in that "visual" box, I have to use CSS to align it to that box, so I have one div that places it in that box, and then I have an other one inside that to make the heading in an other font (that one is not placing it in any position. But in IE, it doesn't care about my inner div, it prints everything according to the outhers property.

The two divs look like this:


h2{
position:absolute;
left:7px;
top:25px;
width:358px;
height:477px;
font-size: 10px;
font-weight: normal;
font-family: Arial, Helvetica, Verdana, sans-serif;
z-index:38;
visibility: visible;
font-size: 11px;
overflow: auto;
padding: 0 0 0 0;
margin: 0 0 0 0;
}

verdana{
font-size: 11px;
font-weight: bold;
font-family: Helvetica, Verdana, Arial, sans-serif;
font-style: normal;
font-variant: normal;
color: #000000;
}


here is a link to the page if you don't understand what I mean. (I am updating all the time, so it might look diffrent from time to time, and even some errors can ocour...;)

http://www.klubbscenen.com/klubbny/

NoteMe
Apr 18th, 2004, 01:47 PM
Not sure how nice it looks, but I fixed the table problem adding

"<p align=right>"

to the text that I are putting in to the right cell...

NoteMe
Apr 18th, 2004, 02:09 PM
I fixed the other problem too. IE don't like CSS and I hate that since most users are using IE....:(

Electroman
Jun 10th, 2004, 01:37 PM
Posted by kows
eww, register_globals is on?

This line should be making your entire thing not work:
if(isset($WINDIR)) $userfile = str_replace("\\\","\\", $userfile);
If you have warnings/errors turned off, you may not have caught that.

I don't know what you mean by having it "not upload until it goes to the next page".. You can add other form elements to your page, and also have your upload function do any uploading, but only if it's necessary. You don't have to have a form that's just for uploading.

If you're interested, this is what I use for uploading things, and with a few modifications you can have a nice uploader..
<?
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$url_this = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$upload_dir = "upload_files/";
$upload_url = $url_dir . "/" . $upload_dir;
$message ="";

if($_FILES['userfile']){
$message = do_upload($upload_dir, $upload_url);
}else{
$message = "Invalid File Specified.";
}
echo $message;

function do_upload($upload_dir, $upload_url){
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_type = $_FILES['userfile']['type'];
$file_size = $_FILES['userfile']['size'];
$result = $_FILES['userfile']['error'];
$file_url = $upload_url.$file_name;
$file_path = $upload_dir."gay" . $file_name;
if($file_name == "") {
$message = "Error: Invalid filename.";
return $message;
}elseif($file_size > 500000){
$message = "Error: Filesize exceeds 500K.";
return $message;
}elseif($file_type == "text/plain") {
$message = "Error: Unable to upload plain text files." ;
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
$message = ($result) ? "File Location: <a href=$file_url>$file_url</a>" : "There was an error while uploading the file.";
return $message;
}
?>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload Image<input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload File">
</form> Sorry to dig up this thread but is there a a reason the size limit is 500KB here? Like is it something top do with timing out or something? Or could I change it to a really big number and it will still work?

NoteMe
Jun 10th, 2004, 03:53 PM
Originally posted by Electroman
Sorry to dig up this thread but is there a a reason the size limit is 500KB here? Like is it something top do with timing out or something? Or could I change it to a really big number and it will still work?


I have no limit on my site now...and it works...but I havn't tried realy big files wither. Not sure about the timeout. I am not that into how a browser works, but I guess it will work ok.