php upload code question

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Mon Nov 17, 2008 3:56 am    Post subject: php upload code question Reply with quote

hey guys im a noob when it comes to all this stuff but i got a question- i wanna change the upload location to E:\\ which is the root directory how do i do that, here is the code /**
* This function sets the directory where to upload the file
* In case of Windows server use the form: c:\\temp\\
* In case of Unix server use the form: /tmp/
*
* @param String Directory where to store the files
*/
function setUploadLocation($dir){
$this->uploadLocation = $dir;
}

function showUploadForm($msg='',$error=''){
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Mon Nov 17, 2008 11:38 am    Post subject: Reply with quote

ok here we go put this on any page being html or what not


Code:
<form action="./upload.php" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
      <button>Upload File</button>
   <p>
</form>


now the upload.php

Code:
<?php
   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
 
   // We'll start handling the upload in the next step
 
?>


just chang the $upload_path = to where you want
if you like that you mit like this too

Code:
<?php
   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
 
   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
 
?>

same as befor just chang the $upload_path = and there you have it
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Mon Nov 17, 2008 12:41 pm    Post subject: Reply with quote

thanx man
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Mon Nov 17, 2008 12:43 pm    Post subject: Reply with quote

n/a/p any time
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Mon Nov 17, 2008 11:55 pm    Post subject: Reply with quote

for some reason it didnt work, i put the first set of code into a html file which i added to the index files in the server and i put the 2nd into a php file called upload.php, i see the upload form and i try too upload but it doesnt work, plus i wanna be able to upload movies and music onto here not just pictures. P.S. i even changed the file size but that didnt work.
Back to top View user's profile Send private message AIM Address MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Mon Nov 17, 2008 11:56 pm    Post subject: Reply with quote

for some reason it didnt work, i put the first set of code into a html file which i added to the index files in the server and i put the 2nd into a php file called upload.php, i see the upload form and i try too upload but it doesnt work, plus i wanna be able to upload movies and music onto here not just pictures. P.S. i even changed the file size but that didnt work.
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Nov 18, 2008 12:16 am    Post subject: Reply with quote

$allowed_filetypes just add exe zip mp3 and so on
and as far as the up load look to your php.ini file to
up the file siz the script will only do what the php.ini will do
so open your php.ini file & look for upload_max_filesize = 2M
up the size of it & all is well

or if you dont want to mess with the ini file you could just use a ini_set(upload_max_filesize 20M); in the code at the top below <?php

$upload_path must be full path to where ever ex: C:/Web/www/upload/
then all is well there
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Tue Nov 18, 2008 3:32 am    Post subject: Reply with quote

im not sure whats going on but its not working because i already changed the php.ini to like 500000m but can u just help me with this script cause it seems to work some what, http://internap.dl.sourceforge.net/sourceforge/webfilebrowser/webfilebrowser-0.4b14.zip

the error i keep getting is, No name for file to upload, im not sure what the problem is whether its a bad extension, or too big of a file or whatever.
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Nov 18, 2008 11:54 am    Post subject: Reply with quote

ok i looked at your file but i do not speck them languages
but i made on that i tested in my server that i know it works

http://rapidshare.com/files/164939077/file.rar.html

i did not change no exstion's i do not know what file types you are working with
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Tue Nov 18, 2008 12:42 pm    Post subject: Reply with quote

im just trying to upload .mp3's and .avi's
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Nov 18, 2008 1:15 pm    Post subject: Reply with quote

Code:
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.mp3','.avi'); // These will be the types of
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Tue Nov 18, 2008 10:11 pm    Post subject: Reply with quote

i already did that lol, ok tell me how to install this maybe i did this wrong, i made a folder called uploader in my html docs and i added index.html to my index files in the console, i opened the uploader chose my mp3 file and clicked upload and nothing happens and when i look in my files there is nothing there.
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Nov 18, 2008 11:22 pm    Post subject: Reply with quote

Override wrote:
ok i looked at your file but i do not speck them languages
but i made on that i tested in my server that i know it works

http://rapidshare.com/files/164939077/file.rar.html

i did not change no exstion's i do not know what file types you are working with

did you dl this file ? if you did'n dl it & put it in your web root
just change the file typs to what you want it works i tested my self
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Wed Nov 19, 2008 10:38 pm    Post subject: Reply with quote

i already added the .mp3 extension but when i click upload file, Nothing happens.....
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Thu Nov 20, 2008 2:07 pm    Post subject: Reply with quote

ok 1 more time i got it to work
ok lay your files like this
in your htdocs
you should have a folder name file & in that folder another folder upload
ok in the file folder index.html and upload.php the code below should
be as.
The index.html
Code:
<form action="./upload.php" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
      <button>Upload File</button>
   <p>
</form>

Ok now the updated code that i know it works upload up to 50MB

upload.php

Code:
<?php
   ini_set('upload_max_filesize', '50M');
   ini_set('post_max_size', '50M');
   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.mp3','.avi'); // These will be the types of file that will pass the validation.
      $max_filesize = 50000000; // Maximum filesize in BYTES (currently 50MB).
      $upload_path = './upload/'; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
 
   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="' . $filename . '">' . $filename . '</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
 
?>

now all should be all good & done p.s i would put a blank index.html file in the upload folder so it cant be browsed for Security bots & so forth


Last edited by Override on Thu Nov 20, 2008 3:13 pm; edited 1 time in total
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Thu Nov 20, 2008 2:58 pm    Post subject: Reply with quote

Now if you what only ppl you want to have authorization for Security
replace the index.html with index.php and the code should be as below.

Code:
<?php

$valid_passwords = array ("Admin" => "adminpass","User" => "userpass");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
  header('WWW-Authenticate: Basic realm="My Realm"');
  header('HTTP/1.0 401 Unauthorized');
  die ("Not authorized");
}

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title></title>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<body>
<div align="center">
<form action="./upload.php" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
      <button>Upload File</button>
   <p>
</form>
</div>
</body>
</html>'
?>


just replase the user names & passwords have as manny as you want
in line
$valid_passwords = array ("Admin" => "adminpass","User" => "userpass");
or do it in the control panel your chose what ever
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
0wnagee
-


Joined: 14 Nov 2008
Posts: 14

PostPosted: Thu Nov 20, 2008 9:21 pm    Post subject: Reply with quote

i did just as u said and it doesnt work- when i click upload nothign happens.. do u have AIM? we can talk on there
Back to top View user's profile Send private message AIM Address MSN Messenger
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Fri Nov 21, 2008 9:54 pm    Post subject: Reply with quote

ok rebuilt the forum was a ie prob the updated v of it should be as below


Code:

<!-- Start of FORM -->
<form method="POST" enctype="multipart/form-data" action="./upload.php">
<label for="file">Select a file:</label> <input type="file" name="userfile" value=""><br />
<button type="submit">Upload File</button>
</form>
<!-- End of FORM -->

all the best
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group