File Upload without HTML form

I have got a function for upload file. However, the functions makes use of the html form. But now, what i get is the path given by the client

function upload($path){
if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 20000000)){
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}

}

i would like to know how to do if i didnt use the HTML form to upload file, how to upload file with the client directory?

Thank you.
[1317 byte] By [dummyagain] at [2007-11-20 8:23:23]
# 1 Re: File Upload without HTML form
You can't. In order to upload a file, you must have a file type <input> tag within a form. There is no other way to upload.
PeejAvery at 2007-11-10 3:56:20 >
# 2 Re: File Upload without HTML form
then what can i do if i only get the directory of the file... my case is that i get a php file for dealing with the upload file function, the file only gets the function to work with..i am wondering if i should place an invisible form but i have no idea how it can trigger the submit function automatically.

Please advice.
dummyagain at 2007-11-10 3:57:20 >
# 3 Re: File Upload without HTML form
You see, without a file input the browser cannot send the file data to the server. Therefore, if you just use a textbox to send something like c:\path\to\file.jpg then it will read as just text. You will notice enctype="multipart/form-data" as part of a form for uploads. That is because the browser itself must encode the file and send it. You cannot do this without a form. Unless you feel like programming your own internet browser, you must use a form.
PeejAvery at 2007-11-10 3:58:29 >
# 4 Re: File Upload without HTML form
ok, i understand... then what i have done now is to make a form and autosubmit it. I am writing sth like

<form id="fileUploadForm" name="form1" enctype="multipart/form-data" method="post" action="processFile.php?cmd=upload">
<input type="file" name="file" value="C:\Documents and Settings\Me\My Documents\My Pictures\us.jpg" />
</form>
<script language="JavaScript" type="text/javascript">
document.getElementById('fileUploadForm').submit();
</script>

However, in the processFile.php, i tried to "echo $_FILES["file"]["type"];" but nth is printed... are there any problems?

Thank you
dummyagain at 2007-11-10 3:59:30 >
# 5 Re: File Upload without HTML form
That is because you can't alter the value attribute for a file input type.

EDIT: To clarify...Any <input> with file as its type does not have a value attribute.
PeejAvery at 2007-11-10 4:00:29 >
# 6 Re: File Upload without HTML form
then is there any way to give the value to the <input type = file name= file> by the system?

Thank you
dummyagain at 2007-11-10 4:01:25 >
# 7 Re: File Upload without HTML form
it may cause a threat if the system allows uploading file without user interaction. ;)
Thread1 at 2007-11-10 4:02:29 >
# 8 Re: File Upload without HTML form
then is there any way to give the value to the <input type = file name= file> by the system?
No. It requires user input to navigate the choose file dialog. As already stated, it would be a security risk. And who knows what is being taken off someone's computer.
PeejAvery at 2007-11-10 4:03:32 >
# 9 Re: File Upload without HTML form
I would like to know if it's possible for me to use PHP file stream to upload the files to server?

Thank you.
dummyagain at 2007-11-10 4:04:28 >
# 10 Re: File Upload without HTML form
No. I have already told you that you must have a file input with a form set to multipart/form-data in order for the PHP to grab it.

You CANNOT upload without a form.
PeejAvery at 2007-11-10 4:05:29 >
# 11 Re: File Upload without HTML form
No. I have already told you that you must have a file input with a form set to multipart/form-data in order for the PHP to grab it.

You CANNOT upload without a form.

On a related issue (atleast from a top level concept...

Can you tell me how to winn the lottery drawing for 10 the next 10 weeks?

(ps: if (code1.Impossible == code2.Impossible) { code2; } :eek: :eek: )
TheCPUWizard at 2007-11-10 4:06:39 >
# 12 Re: File Upload without HTML form
To explain a little more...

PHP is on the server-side. You must remember that the server-side is bound by many restrictions. One being...it cannot talk to the client unless the client initiates the connection. In this case, for PHP to access $_FILES it must be triggered by a form with and <input> with the type set to file.
PeejAvery at 2007-11-10 4:07:32 >
# 13 Re: File Upload without HTML form
furthermore, since the browser does not allow it for security reasons, might want to create your own http client/upload program :D hope that helps :wave:
Thread1 at 2007-11-10 4:08:38 >