Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

PHP - I need some help, please.


Tommo
 Share

Recommended Posts

I am working on a project at the moment, which involves some php.

I don't know anything at all about php, but i have managed to get a file upload script to work:

 

<?php
  // Configuration - Your Options
     $allowed_filetypes = array('.wma','.mp3','.wav'); // These will be the types of file that will pass the validation.
     $max_filesize = 1048576; // 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. Please only use .mp3, .wav or .wma');

  // 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

   // Upload the file to your specified path.
  if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
        echo 'Your file upload was successful, click <a href="index.html">here</a> to go back home '; // It worked.
     else
        echo 'There was an error during the file upload.  Please try again.'; // It failed .

?>

 

It does everything perfectly as i want it, besides one thing. I would like it to randomize the file names, once they have been uploaded.

I have found this website, which has got something to do with randomizing. But i dont know how to put the two pieces of code together, so that they can work without conflicting eachother.

Link to comment
Share on other sites

This should work.

 

<?php
  // Configuration - Your Options
     $allowed_filetypes = array('.wma','.mp3','.wav'); // These will be the types of file that will pass the validation.
     $max_filesize = 1048576; // 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. Please only use .mp3, .wav or .wma');

  // 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.');


  // random 4 digit to add to our file name
  // some people use date and time in stead of random digit
  $random_digit=rand(0000,9999);

  //combine random digit to you file name to create new file name
  //use dot (.) to combile these two variables

  $new_file_name=$random_digit.$filename;


  // We'll start handling the upload in the next step

  // Upload the file to your specified path.
  if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $new_file_name))
        echo 'Your file upload was successful, click <a href="index.html">here</a> to go back home '; // It worked.
     else
        echo 'There was an error during the file upload.  Please try again.'; // It failed .

?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share