Jump to content



  • Please log in to reply
2 replies to this topic

#1 Perad

  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 27 October 2006 - 02:02 PM

  $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); 

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?

#2 obsidian

  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 October 2006 - 02:09 PM

This page in the manual deals with using predefined variables such as $_FILES. You can also find information on exactly how to do what you're after on the Handling file uploads page of the manual.

Good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 arianhojat

  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 27 October 2006 - 02:15 PM

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....html#msg428434

Code Example:
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

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.';
				echo "Yay Uploaded: tmp_name=$tmp_name, name=$name, type=$type, size=$size<br/>" ;

	        echo 'Couldn\'t Upload File';	



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

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

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



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users