Jump to content

Noob ?? - Help with File Upload Form


FSGDAG

Recommended Posts

Hi...

 

I'm very new to programming with PHP. I'm programming in PHP v 5 and I'm trying to create a file upload form. I have two files that I'm using. An HTML page and a PHP page. The HTML page allows for you to choose the file(s) that you want and a submit button, while the php file processes the files. Here are the contents of each:

 

HTML page:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
	<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
	Choose a file to upload: <input name="uploadedfile1" type="file" /><br />
	Choose a file to upload: <input name="uploadedfile2" type="file" /><br />
	Choose a file to upload: <input name="uploadedfile3" type="file" /><br />
	Choose a file to upload: <input name="uploadedfile4" type="file" /><br />
	Choose a file to upload: <input name="uploadedfile5" type="file" /><br />
	<br /><br />
	<input type="Submit" value="Upload File" />
	</form>

 

My PHP file is :

<?php

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

/* Upload File 1 - Add the original filename to our target path.
Result is "uploads/filename.ext" */
$target_path=$target_path . basename($_FILES['uploadedfile1']['name']);

if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path)) {
echo "The File ". basename($_FILES['uploadedfile1']['name']).
" has been uplaoded";
} else {
echo "There was an error uploading the file 1, please try again!";
}

/* Upload File 2 - Add the original filename to our target path.
Result is "uploads/filename.ext" */
$target_path=$target_path . basename($_FILES['uploadedfile2']['name']);

if(move_uploaded_file($_FILES['uploadedfile2']['tmp_name'], $target_path)) {
echo "The File ". basename($_FILES['uploadedfile2']['name']).
" has been uplaoded";
} else {
echo "There was an error uploading the file 2, please try again!";
}

/* Upload File 3 - Add the original filename to our target path.
Result is "uploads/filename.ext" */
$target_path=$target_path . basename($_FILES['uploadedfile3']['name']);

if(move_uploaded_file($_FILES['uploadedfile3']['tmp_name'], $target_path)) {
echo "The File ". basename($_FILES['uploadedfile3']['name']).
" has been uplaoded";
} else {
echo "There was an error uploading the file 3, please try again!";
}

/* Upload File 4 - Add the original filename to our target path.
Result is "uploads/filename.ext" */
$target_path=$target_path . basename($_FILES['uploadedfile4']['name']);

if(move_uploaded_file($_FILES['uploadedfile4']['tmp_name'], $target_path)) {
echo "The File ". basename($_FILES['uploadedfile4']['name']).
" has been uplaoded";
} else {
echo "There was an error uploading the file 4, please try again!";
}

/* Upload File 5 - Add the original filename to our target path.
Result is "uploads/filename.ext" */
$target_path=$target_path . basename($_FILES['uploadedfile5']['name']);

if(move_uploaded_file($_FILES['uploadedfile5']['tmp_name'], $target_path)) {
echo "The File ". basename($_FILES['uploadedfile5']['name']).
" has been uplaoded";
} else {
echo "There was an error uploading the file 5, please try again!";
}


?>

 

All 5 files upload but the problem is that File name 1 appends to file name 2, file name 3 has file name 1 and 2 before it. So it would look like this:

 

File Name 1 = One.csv

File Name 2 = Two.csv

File Name 3 = Three.csv

File Name 4 = Four.csv

File Name 5 = Five.csv

 

But in the upload folder on the server, the file names are like this:

 

File Name 1 = One.csv

File Name 2 = One.csvTwo.csv

File Name 3 = One.csvTwo.csvThree.csv

File Name 4 = One.csvTwo.csvThree.csvFour.csv

File Name 5 = One.csvTwo.csvThree.csvFour.csvFive.csv

 

Where am I going wrong in the php file that it keeps appending the previous file name to the next file name?

 

Thanks in advance for any help you can give this noob! :)

Link to comment
https://forums.phpfreaks.com/topic/238414-noob-help-with-file-upload-form/
Share on other sites

With each file, you're adding the name of the next file every time you do this:

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

 

You might consider changing it so it doesn't reassign the value of the $target_path variable with each file, or use a constant. I didn't test this, but I can't think of a reason off the top of my head why it shouldn't work.

define('TARGET_PATH', 'uploads/');

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

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.