Tuesday, July 28, 2009

How to make a file thats uplaoded get sent to a specific folder on my server in PHP?

I can make upload forms in php that upload images to my server but what do i write for where i want their file to go.


I want the file to get sent to a folder called Uploads so what do i write in the file location. does it need to start with a "/" and end in a "/" as it never works.

How to make a file thats uplaoded get sent to a specific folder on my server in PHP?
Here you go. Note the "C:\\Temp\\" is the upload path on the server that I have set. Replace that with the path on your server. Also not the two backslashes. The first is to excape the second. Here is the code


%26lt;?php


if (isset($_POST['submit'])){


//you will need to change the path to where you want to upload to.


$uploadDirFile = "C:\\Temp\\" . basename( $_FILES['uploadedfile']['name']);


$_FILES['uploadedfile']['name'];


$uploadMsg = "";


if(move_uploaded_file($_FILES['uploade... $uploadDirFile)) {


$uploadMsg .= "The file ". basename( $_FILES['uploadedfile']['name']).


" has been uploaded";


} else{


$uploadMsg .= "There was an error: " . error_reporting(2039) . " uploading the file, please try again!";


}


}


?%26gt;


%26lt;html%26gt;


%26lt;head%26gt;%26lt;title%26gt;Upload File%26lt;/title%26gt;%26lt;/head%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;div align="center"%26gt;


%26lt;table width="550" border="0" cellpadding="0" cellspacing="0"%26gt;


%26lt;tr%26gt;


%26lt;td colspan="2" class="PageText"%26gt;


%26lt;table width="100%" border="0" cellpadding="0" cellspacing="0"%26gt;


%26lt;tr%26gt;


%26lt;td%26gt;


%26lt;?php echo $uploadMsg; ?%26gt;


%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;tr%26gt;


%26lt;td%26gt;


%26lt;div%26gt; %26lt;/div%26gt;


%26lt;FORM ENCTYPE="multipart/form-data" ACTION="%26lt;?=$_SERVER['PHP_SELF']?%26gt;" METHOD="POST"%26gt;


%26lt;INPUT TYPE="HIDDEN" NAME="MAX_FILE_SIZE" VALUE="30000"%26gt;


Upload File: %26lt;INPUT NAME="uploadedfile" TYPE="FILE"%26gt;



%26lt;INPUT TYPE="SUBMIT" NAME="submit" VALUE="Upload"%26gt;


%26lt;/FORM%26gt;


%26lt;div%26gt; %26lt;/div%26gt;


%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;/table%26gt;


%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;/table%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;
Reply:PHP stores the file in a temporary location -- you don't really have much control over that. But once it is uploaded, you can move it to where you want it using (unsurprisingly) "move_uploaded_file"





See here:





http://us3.php.net/manual/en/function.mo...
Reply:Read documentation for the move_uploaded_file() function:





http://www.php.net/move_uploaded_file





___________


No comments:

Post a Comment