Jump to content

upload files


dare87

Recommended Posts

I'm trying to make a page so I can upload music compositions I've created. I'm new to php and I've toyed with things I've found online, but can't get anything to work.

 

This is what I have so far

 

<?php
			// Where the file is going to be placed 
			$target_path = "music/";

			/* Add the original filename to our target path.  
			Result is "music/filename.extension" */
			$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
			$_FILES['uploadedfile']['tmp_name'];  

			$target_path = "music/";

			$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

			if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
			     echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
			    " has been uploaded"; 
			} else{
			    echo "";
			}
			?>

			<form enctype="multipart/form-data" action="musicupload.php" method="POST">
			<input type="hidden" name="MAX_FILE_SIZE" value="2500000000" />
			Choose a file to upload: <input name="uploadedfile" type="file" /><br />
			<input type="submit" value="Upload File" />
			</form>

 

The problem I'm having is that I need to have the data stored in a DB... such as the name, filetype, tmp name...

 

I don't know how or where to add that. I would also like it to say if it didn't upload the file, but again, I don't know where or how.

 

and lastly, what I have now doesn't upload the files that I have tried.. I need to be able to upload 6mb-16mb files.

 

Thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/134841-upload-files/
Share on other sites

Hi Mate,

 

You were getting error number 2:

 

Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. 

 

musicupload.php

 

<?php
if (isset($_POST['submit-upload']))
{   
    // DEBUG
    //print_r($_FILES);

$fileTemp = $_FILES['uploadedfile']['tmp_name'];  
    $fileName = $_FILES['uploadedfile']['name']; 

// Where the file is going to be placed 
$target_path = "music/$fileName";

if(move_uploaded_file($fileTemp, $target_path)) {
		echo "The file <b>$fileName</b> has been uploaded"; 
		exit;
} else{
		echo "There was an error uploading the file.";
		exit;
}
} else {
?>				
			<form enctype="multipart/form-data" action="musicupload.php" method="POST">
			Choose a file to upload: <input name="uploadedfile" type="file" /><br />
			<input type="submit" name="submit-upload" value="Upload File" />
			</form>
<?php } ?>

 

This will upload your file! just need to make sure the sizes are correct and validate the extensions and file types :)

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/134841-upload-files/#findComment-702181
Share on other sites

Ok, I've gotten a little further with what I am doing, but I'm still having some problems.

 

1. I can't get files over 1-2mb to upload

2. I need the file that's posted to be named the same thing or something similar to the id in the db

 

here is what I have so far...

 

<?php
			if (isset($_POST['submit-upload']))
			{   
				// DEBUG
				//print_r($_FILES);

				$fileTemp = $_FILES['uploadedfile']['tmp_name'];  
				$fileName = $_FILES['uploadedfile']['name'];

				// Where the file is going to be placed 
				$target_path = "music/$fileName";
				if (!empty($_REQUEST['comment']))
					$comment = $_REQUEST['comment'];

				if(move_uploaded_file($fileTemp, $target_path)) {

					require_once('mysql_connect.php');
					$query = "INSERT INTO music SET name='$fileName', comment='$comment'";
					$results = @mysql_query($query);

						echo "The file <b>$fileName</b> has been uploaded"; 
				} else{
						echo "There was an error uploading the file.";
				}
			} else {
		?>				
				<form enctype="multipart/form-data" action="music_upload.php" method="POST">
                    <table align="center" width="100%" border="0" cellpadding="1" cellspacing="0">
				<tr>
					<td colspan="2"><b>Add Music</b></td>
				</tr>
				<tr>
					<td>File:</td>
					<td><input type="hidden" name="MAX_FILE_SIZE" value="20000000" /><input name="uploadedfile" class="required" type="file" /></td>
				</tr>
				<tr>
					<td>Display Name:</td>
					<td><input type="text" class="required" name="comment" id="focus" size="40" value="<?php echo $comment; ?>" maxlength="40"></td>
				</tr>
                    <tr>
					<td> </td>
					<td><input type="submit" class="button" name="submit-upload" value="Upload File" /></td>
				</tr>
				<tr>
					<td> </td>
					<td class="smallText">* Highlighted forms designate required fields.</td>
				</tr>
				</table>
				</form>
		<?php } ?>

 

Thanks for the help thus far...

 

Table setup...

id int(10) auto_increment, primary key

comment varchar(50)

name varchar(50)

type varchar(50)

size int(10)

Link to comment
https://forums.phpfreaks.com/topic/134841-upload-files/#findComment-702696
Share on other sites

Hi Mate,

 

Here is the code with a few minor changes and  atiny bit of validation:

 

<?php
if (isset($_POST['submit-upload']))
{   
    // LET'S DO A LITTLE VALIDATION FOR THE FILE	
    // THESE VARS ARE THE TEMP AND ORIGINAL NAME OF THE FILE							 
$fileTemp = $_FILES['uploadedfile']['tmp_name'];  
$fileName = $_FILES['uploadedfile']['name'];

// THIS ONE IS FOR THE TYPE OF FILE UPLOADED .MP3 = audio/mpeg
$fileType = $_FILES['uploadedfile']['type'];

    // IF THE TYPE ISN'T AUDIO/MPEG THEN...
    if ($fileType != "audio/mpeg")
    {
      print("The file you tried to upload doesn't seem to be an .mp3 please try again.");
      exit;
    }

// Where the file is going to be placed 
$target_path = "music/$fileName";

// IF THE COMMENT FIELD ISN'T EMPTY RECORD IT
if (!empty($_POST['comment']))
{
	$comment = $_POST['comment'];
    }

if(move_uploaded_file($fileTemp, $target_path)) {

// REQUIRE THE DATABASE CONNECTION 
require_once('mysql_connect.php');

// INSERT THE DATA INTO MYSQL
$query = "INSERT INTO `music` SET `name`='$fileName',`comment`='$comment'";
$results = @mysql_query($query);

echo "The file <b>$fileName</b> has been uploaded"; 

} else {

echo "There was an error uploading the file.";
}

} else {
?>				
				<form enctype="multipart/form-data" action="index.php" method="POST">
                    <table align="center" width="100%" border="0" cellpadding="1" cellspacing="0">
				<tr>
					<td colspan="2"><b>Add Music</b></td>
				</tr>
				<tr>
					<td>File:</td>
					<td><!--<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />--><input name="uploadedfile" class="required" type="file" /></td>
				</tr>
				<tr>
					<td>Display Name:</td>
					<td><input type="text" class="required" name="comment" id="focus" size="40" value="<?php echo $comment; ?>" maxlength="40"></td>
				</tr>
                    <tr>
					<td> </td>
					<td><input type="submit" class="button" name="submit-upload" value="Upload File" /></td>
				</tr>
				<tr>
					<td> </td>
					<td class="smallText">* Highlighted forms designate required fields.</td>
				</tr>
				</table>
				</form>
<?php } ?>

 

as for question 1, you will need to set this:

 

upload_max_filesize = 2M;  <-- in your php.ini file on yourr server to say... 10 to be able to upload 10mb etc are you on a dedicated host or shared? if shared they may not up that for you then again they might :)

 

"I need the file that's posted to be named the same thing or something similar to the id in the db"

 

are you working your site with sessions or cookies? there is loads of ways you could rename the file does it need to be by the id?

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/134841-upload-files/#findComment-703229
Share on other sites

Ok, I've updated my code with what you have...

 

<?php
		if (isset($_POST['submit-upload']))
		{   
			// LET'S DO A LITTLE VALIDATION FOR THE FILE	
			// THESE VARS ARE THE TEMP AND ORIGINAL NAME OF THE FILE							 
			$fileTemp = $_FILES['uploadedfile']['tmp_name'];  
			$fileName = $_FILES['uploadedfile']['name'];

			// THIS ONE IS FOR THE TYPE OF FILE UPLOADED .MP3 = audio/mpeg
			$fileType = $_FILES['uploadedfile']['type'];

			// IF THE TYPE ISN'T AUDIO/MPEG THEN...
			if ($fileType != "audio/mpeg")
			{
			  print("The file you tried to upload doesn't seem to be an .mp3 please try again.");
			}

			// Where the file is going to be placed 
			$target_path = "music/$fileName";

			// IF THE COMMENT FIELD ISN'T EMPTY RECORD IT
			if (!empty($_POST['comment']))
			{
				$comment = $_POST['comment'];
			}

			if (!empty($_POST['intext']))
			{
				$intext = $_POST['intext'];
			}

			if(move_uploaded_file($fileTemp, $target_path)) {

			// REQUIRE THE DATABASE CONNECTION 
			require_once('mysql_connect.php');

			// INSERT THE DATA INTO MYSQL
			$query = "INSERT INTO music SET name='$fileName', comment='$comment', intext='$intext'";
			$results = @mysql_query($query);

			echo "The file <b>$fileName</b> has been uploaded"; 

			} else {

			echo "There was an error uploading the file.";
			}

			} else {
		?>				
                    <form enctype="multipart/form-data" action="music_upload.php" method="POST">
                    <table align="center" width="100%" border="0" cellpadding="1" cellspacing="0">
                    <tr>
                        <td colspan="2"><b>Add Music</b></td>
                    </tr>
                    <tr>
                        <td>File:</td>
                        <td><!--<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />--><input name="uploadedfile" class="required" type="file" /></td>
                    </tr>
                    <tr>
                        <td>Display Name:</td>
                        <td><input type="text" class="required" name="comment" id="focus" size="40" value="<?php echo $comment; ?>" maxlength="40"></td>
                    </tr>
                    <tr>
                        <td>Public/Private:</td>
                        <td><select class="required" name="intext">
                            <option value="">SELECT</option>
                            <option value="0">Private</option>
                            <option value="1">Public</option>
                            </select></td>
                    </tr>
                    <tr>
                        <td> </td>
                        <td><input type="submit" class="button" name="submit-upload" value="Upload File" /></td>
                    </tr>
                    <tr>
                        <td> </td>
                        <td class="smallText">* Highlighted forms designate required fields.</td>
                    </tr>
                    </table>
                    </form>
		<?php } ?>

 

I changed the php.ini to 20mb and it worked, the only problem is I'm moving hosts shortly... I hope I can change the php.ini there too.

 

As for the rename. I use sessions. I want it renamed in can I happen to upload the same file twice, the reason being is that I change little things in each file, but I sometimes leave them as the same name.  If I could change it to a number (Which is why I was thinking the id) and have each file that is uploaded be a different number.

 

when it posted the information to the DB I want the newname, name when it was uploaded, and the display name...

 

And the validation gives the error, but it still uploaded files with different extentions... any ideas.

 

Thanks for your help thus far graham23s

 

 

Link to comment
https://forums.phpfreaks.com/topic/134841-upload-files/#findComment-703428
Share on other sites

Hi Mate,

 

you could do this:

 

<?php
    // RENAME THE FILE USING A TIME STAMP SO IT WILL NEVER BE THE SAME AND APPEND A RANDOM NUMBER
    $randomNumber = rand(1, 999999);
    $newFileName = time() . $randomNumber . ".mp3";

// Where the file is going to be placed 
$target_path = "music/$newFileName";
?>

 

so it will look kinda like: 1228170121904480.mp3

 

that way they will never be the same and over write each other :)

 

Graham

 

complete:

 

<?php
		if (isset($_POST['submit-upload']))
		{   
			// LET'S DO A LITTLE VALIDATION FOR THE FILE	
			// THESE VARS ARE THE TEMP AND ORIGINAL NAME OF THE FILE							 
			$fileTemp = $_FILES['uploadedfile']['tmp_name'];  
			$fileName = $_FILES['uploadedfile']['name'];

			// THIS ONE IS FOR THE TYPE OF FILE UPLOADED .MP3 = audio/mpeg
			$fileType = $_FILES['uploadedfile']['type'];

			// IF THE TYPE ISN'T AUDIO/MPEG THEN...
			if ($fileType != "audio/mpeg")
			{
			  print("The file you tried to upload doesn't seem to be an .mp3 please try again.");
			}

			// RENAME THE FILE USING A TIME STAMP SO IT WILL NEVER BE THE SAME AND APPEND A RANDOM NUMBER
                $randomNumber = rand(1, 999999);
                $newFileName = time() . $randomNumber . ".mp3";

            // Where the file is going to be placed 
            $target_path = "music/$newFileName";

			// IF THE COMMENT FIELD ISN'T EMPTY RECORD IT
			if (!empty($_POST['comment']))
			{
				$comment = $_POST['comment'];
			}

			if (!empty($_POST['intext']))
			{
				$intext = $_POST['intext'];
			}

			if(move_uploaded_file($fileTemp, $target_path)) {

			// REQUIRE THE DATABASE CONNECTION 
			require_once('mysql_connect.php');

			// INSERT THE DATA INTO MYSQL
			$query = "INSERT INTO music SET name='$fileName', comment='$comment', intext='$intext'";
			$results = @mysql_query($query);

			echo "The file <b>$fileName</b> has been uploaded"; 

			} else {

			echo "There was an error uploading the file.";
			}

			} else {
		?>				
                    <form enctype="multipart/form-data" action="index.php" method="POST">
                    <table align="center" width="100%" border="0" cellpadding="1" cellspacing="0">
                    <tr>
                        <td colspan="2"><b>Add Music</b></td>
                    </tr>
                    <tr>
                        <td>File:</td>
                        <td><!--<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />--><input name="uploadedfile" class="required" type="file" /></td>
                    </tr>
                    <tr>
                        <td>Display Name:</td>
                        <td><input type="text" class="required" name="comment" id="focus" size="40" value="<?php echo $comment; ?>" maxlength="40"></td>
                    </tr>
                    <tr>
                        <td>Public/Private:</td>
                        <td><select class="required" name="intext">
                            <option value="">SELECT</option>
                            <option value="0">Private</option>
                            <option value="1">Public</option>
                            </select></td>
                    </tr>
                    <tr>
                        <td> </td>
                        <td><input type="submit" class="button" name="submit-upload" value="Upload File" /></td>
                    </tr>
                    <tr>
                        <td> </td>
                        <td class="smallText">* Highlighted forms designate required fields.</td>
                    </tr>
                    </table>
                    </form>
		<?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/134841-upload-files/#findComment-703458
Share on other sites

Archived

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

×
×
  • Create New...

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.