FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Upload a preset file using PHP





wormhole.v2
Hey.

Ok, here's my problem: I know how to make a simple upload script (where a user can browse for a file, click submit and it gets uploaded) but how do make a script that will upload a file to which I give the path in advance, i.e. in the script itself (e.g. "C:\sometext.txt"), without going through making a form and posting?

I really need this and I'd really appreciate any help I get.

Thanks.
avk
here is the required Code..

Quote:


<?
<!-- put all in upload.php file -->
<? echo"<form action=\"$self?page=upelibimage\" method=\"post\" enctype=\"multipart/form-data\">"; ?>
Browse the e-library image to Upload: <i>Size of image must be 1MB or less. </i><br>
<input name="filetoupload" type="file" class="text1"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<input type="Submit" class="text1" value="Upload Card">
<?php
/* Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.

So if I filetoupload the file 'test.doc', the $_FILES['upload']['name']
would be 'phptut.doc' and $_FILES['upload']['type'] would be 'application/msword'.
---------------------------------------------------------------------*/
// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "../elibrary/images/"; //change to whatever you want.
// files less than 1MB
$size_bytes = 1048576; //bytes will be uploaded
//check if the directory exist or not.
if (!is_dir("$upload_dir")) {
die ("The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
}

//Check first if a file has been selected
//is_filetoupload_file('filename') returns true if
//a file was filetoupload via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{

//Get the Size of the File
$size = $_FILES['filetoupload']['size'];
//Make sure that $size is less than 1MB (1000000 bytes)
if ($size > $size_bytes)
{
echo "File Too Large. Please try again.";
exit();

}
// $filename will hold the value of the file name submetted from the form.
$filename = $_FILES['filetoupload']['name'];
// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filename)){
echo "Oops! The file named <b>$filename </b>already exists";
exit();
}

//Move the File to the Directory of your choice
//move_filetoupload_file('filename','destination') Moves an filetoupload file to a new location.
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {

//tell the user that the file has been filetoupload
echo "File (<a href=$upload_dir$filename>$filename</a>) uploaded!";


}
else
{
//Print error
echo "There was a problem moving your file";
exit();

}
}

?>
</form>

Ranfaroth
wormhole.v2 wrote:
but how do make a script that will upload a file to which I give the path in advance
You can't, for obvious security considerations.
wormhole.v2
@avk
I know how to do that, what I want is a way to upload a file my doing something like:
http://whatever.site.com/script.php?file=C:<text.txt
And it uploads the given file from the browser's computer.

@Ranfaroth
There has to be a way...
Ok can you maybe explain to me what happens when click the submit button of an upload form?
Would there be any way to simulate it?

I really need this and thanks so far to both of you for replying.
Ranfaroth
wormhole.v2 wrote:
There has to be a way...
No, I've already explained why.
Quote:
Ok can you maybe explain to me what happens when click the submit button of an upload form?
You mean the HTTP protocole of a POST request ?
Quote:
Would there be any way to simulate it?
Yes, but not client-side.
Quote:
I really need this
You'll have to find something else.
Why don't just ask the user to upload the file ?
Related topics
how to compress a file using php ?
How to upload a file via html-form and php?
Open, Write to and Close a txt file using php
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Using php to change content of a .txt
Can I add tasks to Windows Scheduled Tasks - Using PHP?
Help displaying pdfs using PHP
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
What is ment by Permission in FriHost File Manager?
Creating a .txt file
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.