Jump to content

[php]uploadingscript


Jack McKalling

Recommended Posts

Hi, this is the first time I start making scripts to upload files. I'm not a php newbee but no expert either. And I work in Xhtml instead of html.This is my code, which is an html test for what will be later an Xhtml page:

<?phpif (empty($_FILES['Movie']) == true) {echo "\r\n<form enctype=\"multipart/form-data\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">",  "\r\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"3000\" />",  "\r\nUpload: <input type=\"file\" name=\"Movie\" /><br />",  "\r\n<button type=\"submit\">Upload!</button>\r\n</form>"; }else {  if (is_uploaded_file($_FILES['Movie']['tmp_name'])) {      copy($_FILES['Movie']['tmp_name'], "/profmemberone/Art/{$_FILES['Movie']['name']}");  } else {      echo "Mogelijke aanval gespot: " . $_FILES['Movie']['name'];  }  echo "Uploaden gelukt! klik hier: <a href=\"/profmemberone/Art/{$_FILES['Movie']['name']}\">Film</a>"; }?>
It seems to be working partly, what have I done wrong?There appears to be no error, while ['error'] returns UPLOAD_ERR_OK.But still the file does not appear in the directory, and if I change the copy() instruction into copy() or die($message); it would show the message :)Can somebody help me?
Link to comment
Share on other sites

No it isn't, while in that directory are more files which are secret. I used it for testing this thing.I created a new folder inside it, changed the urls inside the code into /profmemberone/Art/movies and chmodded that one into 777, but still doesn't work :)

Link to comment
Share on other sites

Thats what I have already as you can see :)But might there be difference between /profmemberone/.. which is my user root directory, and /usr/htdocs/.. something that is the root of the server? (both begin with a slash and point to 'root')[*Edit:] No maxfile size is 3000, and my file is 2000 something :) (I checked with $_FILES['Movie']['size'])

Edited by Dan The Prof
Link to comment
Share on other sites

I mean if the script would be at www.site.com/script/myscript.phpand the target folder would be www.site.com/profmemberone/Art/movies/then use ../profmemberone/Art/movies/

Link to comment
Share on other sites

I don't understand that one.My host is http://dhost.info/ and my root directory is http://dhost.info/profmemberone.The uploading file is located in that root, and Art/movies also is. So the uploading file is this:http://dhost.info/profmemberone/file.phpAnd the target of the uploaded file is this:http://dhost.info/profmemberone/Art/moviesSo, do you mean my relative url in this case would be Art/movies?

Link to comment
Share on other sites

Should there be a dot infront? What does a single dot mean? It didn't work with that relative url, and now I have tried using errors like this:

<?phpif (empty($_FILES['userfile']) == true) {echo "\r\n<form enctype=\"multipart/form-data\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">",  "\r\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />",  "\r\nUpload: <input type=\"file\" name=\"userfile\" /><br />",  "\r\n<button type=\"submit\">Upload!</button>\r\n</form>"; }else {  if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./Art/movies/{$_FILES['userfile']['name']}"))  { echo "Uploaden gelukt! klik hier: <a href=\"/profmemberone/Art/movies/{$_FILES['userfile']['name']}\">Film</a>";  } else  { echo "Uploaden mislukt: " . $_FILES['userfile']['name'] ."<br />\r\n";    if ($_FILES['userfile']['size'] > 3000) echo "File too big!";    elseif ($_FILES['userfile']['type'] != "text/plain") echo "No correct filetype!";    elseif ($_FILES['userfile']['error'] != 0) echo "error: ".$_FILES['userfile']['error'];    elseif (file_exists("{$_FILES['userfile']['tmp_name']}") == false) echo "File not received!";    elseif (file_exists("/profmemberone/Art/movies/{$_FILES['userfile']['name']}") == false) echo "Moving file failed!";    else print_r($_FILES);  }}?>
(the red coloured text appeared after trying) Edited by Dan The Prof
Link to comment
Share on other sites

<?phpif (empty($_FILES['userfile']) == true) {echo "\r\n<form enctype=\"multipart/form-data\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">",  "\r\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />",  "\r\nUpload: <input type=\"file\" name=\"userfile\" /><br />",  "\r\n<button type=\"submit\">Upload!</button>\r\n</form>"; }else {  if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./Art/{$_FILES['userfile']['name']}"))  { echo "Uploaden gelukt! klik hier: <a href=\"/sabin/Art/{$_FILES['userfile']['name']}\">Film</a>";  } else  { echo "Uploaden mislukt: " . $_FILES['userfile']['name'] ."<br />\r\n";    if ($_FILES['userfile']['size'] > 30000) echo "File too big!";    elseif ($_FILES['userfile']['type'] != "text/plain") echo "No correct filetype!";    elseif ($_FILES['userfile']['error'] != 0) echo "error: ".$_FILES['userfile']['error'];    elseif (file_exists("{$_FILES['userfile']['tmp_name']}") == false) echo "File not received!";    elseif (file_exists("/sabin/Art/{$_FILES['userfile']['name']}") == false) echo "Moving file failed!";    else print_r($_FILES);  }}?>

This code just worked for me.I have the script at www.site.com/sabin/file.phpthe target folder at www.site.com/sabin/Art/ and set to 777.(also increased to 30000 because I couldn't find fast a <3000 bytes txt :) )

Link to comment
Share on other sites

Didn't work :) It still says "Moving File Failed!"I'd like to give the actual link to this file, but someoneelse might use it to corrupt my directory :) (in the link I gave earlier it had a different name)

Link to comment
Share on other sites

As far as I know you have to specify the absolute path, which is the server path, not the http or virtual directory path. It has to be the true path. Print the value of the constant __FILE__ to see where your script is located. If you are creating error logs, you can use the constants __FILE__ and __LINE__ to say which file and line the error happened on.

Link to comment
Share on other sites

The uploading part of the script works for me now, but I notice the file has not standard permissions 644, so I want to CHMOD it to 644.So I edit the code with a little help of someone at the forums of my host, it worked, but the next day it didn't :)Whats wrong with this code:

<?phpif (empty($_FILES['userfile']) == true) {  echo "\r\n<form enctype=\"multipart/form-data\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">",    "\r\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />",    "\r\n<input type=\"file\" class=\"text\" name=\"userfile\" /> ",    "\r\n<button type=\"submit\" class=\"button\">Upload!</button>\r\n</form>";}elseif (is_uploaded_file($_FILES['userfile']['tmp_name']) == false) echo("File not received!");elseif (!ereg("^(video/x-msvideo|video/x-ms-wmv|image/gif)$",$_FILES['userfile']['type'])) echo("No correct filetype!");elseif ($_FILES['userfile']['size'] > 30000) echo("File too big!");elseif ($_FILES['userfile']['error'] != 0) echo("Error: ".$_FILES['userfile']['error']);else{ if (move_uploaded_file($_FILES['userfile']['tmp_name'], "../Art/movies/{$_FILES['userfile']['name']}"))  { if (!chmod("Art/movies/{$_FILES['userfile']['name']}",0644)) echo("Permissioning file failed!");    else echo("Uploaden gelukt! klik hier: <a href=\"/profmemberone/Art/movies/{$_FILES['userfile']['name']}\">Film</a>");  } else {    echo "Uploaden mislukt: " . $_FILES['userfile']['name'] ."<br />\r\n";    if (file_exists("/profmemberone/Art/movies/{$_FILES['userfile']['name']}") == false)      echo("Moving file failed!");    else print_r($_FILES);  }}?>
(the red will show up on trial)
Link to comment
Share on other sites

Thank you very much!! :blink: :)It was just all confusing, all those links and changes that were made, its just a simple parentdirectory sign.. :blink: lol, thanks again :) This topic may be closed :) (all works now, completely and entirely)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...