Jump to content

Archived

This topic is now archived and is closed to further replies.

Perad

$_FILES

Recommended Posts

[code]  $numoffile = 1;
  $file_dir  = $_SERVER['DOCUMENT_ROOT']."/files/new/";
  if ($_POST) {
    for ($i=0;$i<$numoffile;$i++) {
      if (trim($_FILES['myfiles']['name'][$i])!="") {
        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
        move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
        $j++;
      }
    }
  } [/code]

I am the master of finding snippets of code  :P. Anyway this is an upload script that i found which will allow the admin to add files to articles.

What is $_FILES? I tried googling to find it but it takes out the $ out of search terms. I tried php.com but that just took to something else called "files"

Could you tell me what it does? Can i use it to get the file extension and file size of an uploaded file?

Share this post


Link to post
Share on other sites
[url=http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.files]This page in the manual[/url] deals with using predefined variables such as $_FILES. You can also find information on exactly how to do what you're after on the [url=http://us3.php.net/manual/en/features.file-upload.php]Handling file uploads[/url] page of the manual.

Good luck!

Share this post


Link to post
Share on other sites
if u have a html form which wants uploading...
u add html form inputs where its type is set to 'file' which will browse to the file on your computer to upload (also if u want to upload in your form, dont forget to add enctype="multipart/form-data" to <form>).
Example of html input:
<input type="file" name="filename[]" />

Note: u can just use name="yourfilename", but i use an array in my example, (name="yourfilenames[]"), since my user can upload many things in same form, then when u submit, php can access the file(s) uploaded with $_FILES array...
so in my php i loop through the $_FILES array and if their are no errors for that file which was uploaded, i move each from temporary upload folder on the server to a directory. But if u r only uploading 1 file, u dont need to loop over it to just move the 1 file.

Here is some background on what could be in that Array... http://www.phpfreaks.com/forums/index.php/topic,106408.msg428434.html#msg428434

Code Example:
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TEST</title>
</head>

<body>
<?php
if( isset($_POST['submitForm']) )
{
echo "SUBMITTING<br/>" ;

foreach ($_FILES["filename"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'\\testFolder\\uploads\\';

$tmp_name = $_FILES["filename"]["tmp_name"][$key];
$name = $_FILES["filename"]["name"][$key];

if( !move_uploaded_file($tmp_name, "$uploaddir$name") )
echo 'Couldn\'t Move File.';
else
{
echo "Yay Uploaded: tmp_name=$tmp_name, name=$name, type=$type, size=$size<br/>" ;
}

}
                else
        echo 'Couldn\'t Upload File';
}


}//submitted

?>

<form name="upload" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>">

File to upload: <br/>
<input type="file" name="filename[]" />
<br/>
<input type="file" name="filename[]" />
<br/>

<br/>
<input type="submit" name="submitForm" value="Submit" />

</form>

</body>
</html>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.