Jump to content

Recommended Posts

Dear Friends,

 

I am using the following code to upload files (3 files at one go in a single page!)

 

move_uploaded_file($_FILES["file".$i]["tmp_name"],"../../Photos/" .$_FILES["file".$i]["name"]);

 

["file".$i] indicates file "upload slots" on the page.

 

I am concerned with ["tmp_name"], is this a string or an indicator to PHP file upload handler, I am worried what will happen if two concurrent user uploads files will the file get mixed up? If so I can try to device a way by replacing ["tmp_name"].

 

Can any one help, thanks.

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/
Share on other sites

Humm can you post the form please,

as normally i would create an array

 

ie

 

<?php

$base_path = "uploads/";


foreach($_FILES['MyFiles']['tmp_name'] as $FN => $File)
{
$target_path = $base_path . basename( $_FILES['MyFiles']['name'][$FN]); 
if(move_uploaded_file($_FILES['MyFiles']['tmp_name'][$FN], $target_path))
{
	echo "The file ".  basename( $_FILES['MyFiles']['name'][$FN])." has been uploaded";
} else{
	echo "There was an error uploading the file, please try again!";
}
}

?>


<form method="post" enctype="multipart/form-data">
<input name="MyFiles[]" type="file" /><br />
<input name="MyFiles[]" type="file" /><br />
<input name="MyFiles[]" type="file" /><br />
<input name="MyFiles[]" type="file" /><br />
<input name="MyFiles[]" type="file" /><br />
<input name="Done" type="submit" value="Upload" />
</form>

 

**UNTESTED and can be improved but you get the idea

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-318554
Share on other sites

MadTechie,

 

Thanks for your hand, my code does not use array (because I am not good at it) simple "for" loop.

 

You are using what I an using

 


if(move_uploaded_file($_FILES['MyFiles']['tmp_name'][$FN], $target_path))

 

what is ['tmp_name'] is it a PHP directive or a string value?

 

I am worried if 2 "racing" pages comes in and one ['tmp_name'] is created and the second overwrites this before this got transfered for the first, do you get my worry ( I am too much but, I am worried)

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-318563
Share on other sites

$_FILES['MyFiles']['tmp_name']

Is the temporary filename of the file in which the uploaded file was stored on the server.

 

this will have a name like "/var/tmp/phpie5H7A" and will should be unique, as the server sets this name  for you.. the problem will be the $target_path name.. as this will be the name of the file being uploaded..

 

so if 2 members upload "myFile.doc" then the last one will over write the previous file..

you can get around this by prefixing the users name/id.

 

ie

$target_path = $base_path . basename( $_FILES['MyFiles']['name'][$FN]); 

 

to

$UserName = 10;//the username or id.
$target_path = $base_path.$UserName."_".basename( $_FILES['MyFiles']['name'][$FN]); 

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-318571
Share on other sites

I don't know that I have to use target path!

 

I only use the following which sits in a for loop

 


move_uploaded_file($_FILES["file".$i]["tmp_name"],"../../Photos/" .$_FILES["file".$i]["name"]);

 

can you write me the target path code for me, but all my files for a particular person will go to a unique folder ( this folder is sitting aling with my other folders)

by the way my php info says "upload_tmp_dir" = "no value" must it set a value. my work is fine even with this setting

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-318580
Share on other sites

I failed!

 

Will you be kind do some twitch to my code, don't any thing major, thanks.

 


			//Make New Folder	
			mkdir("../../Photos/".$RandomKey);
			//Upload Files
			for ($i=0; $i<=$MaxFileCount; $i++)
			{
				if (!($_FILES["file".$i]["name"]==""))
				{	
					if ($_FILES["file".$i]["error"] > 0)
					{
						$UploadFileReadyFlag=2;
						echo "<br />File Error Return Code: " . $_FILES["file".$i]["error"] . "<br /><br />";
					}
					else
					{
						/*Photo Uploads*/
						{
							$FileCount=$FileCount+1;
							move_uploaded_file($_FILES["file".$i]["tmp_name"],"../../Photos/".$RandomKey."/" .$_FILES["file".$i]["name"]);
							rename("../../Photos/".$RandomKey."/" .$_FILES["file".$i]["name"],"../../Photos/".$RandomKey."/" .$UploadFileNameLeader.$FileCount.".jpg");
							/*Check for Width & Height*/
							list($width, $height)=getimagesize("../../Photos/".$RandomKey."/" .$UploadFileNameLeader.$FileCount.".jpg");
							if (($width>100)||($height>100))
							{
								$UploadFileReadyFlag=2;
								echo "File (".$_FILES["file".$i]["name"].") - Is ( ".$width." Width X ".$height." Heigth ) Pixels!<br />";
							}
						}
					}
				}
			}

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-318604
Share on other sites

heres a little update.

 

what are the errors ?

 

maybe try changing it to

upload_tmp_dir = "c:/temp/php/uploads/" (what ever the upload path is)

your need to restart apache

 

<?php
echo "<pre>";
print_r($_FILES);
			//Make New Folder	
			$baseFolder = "../../Photos/".$RandomKey;
			mkdir($baseFolder);
			chmod($baseFolder, 0777); 
			//Upload Files
			for ($i=0; $i<=$MaxFileCount; $i++)
			{
				if ($_FILES["file".$i]["name"]!="")
				{	
					if ($_FILES["file".$i]["error"] > 0)
					{
						$UploadFileReadyFlag=2;
						echo "<br />File Error Return Code: " . $_FILES["file".$i]["error"] . "<br /><br />";
					}
					else
					{
						$FileCount=$FileCount+1;
						move_uploaded_file($_FILES["file".$i]["tmp_name"],$baseFolder."/" .$_FILES["file".$i]["name"]);
						rename($baseFolder."/" .$_FILES["file".$i]["name"],$baseFolder.$UploadFileNameLeader.$FileCount.".jpg");
						/*Check for Width & Height*/
						list($width, $height)=getimagesize($baseFolder."/" .$UploadFileNameLeader.$FileCount.".jpg");
						if (($width>100)||($height>100))
						{
							$UploadFileReadyFlag=2;
							echo "File (".$_FILES["file".$i]["name"].") - Is ( ".$width." Width X ".$height." Heigth ) Pixels!<br />";
						}
					}
				}
			}
?>

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-318629
Share on other sites

  • 2 months later...

Dear people,

 

I have one more issue here.

 

When someone selects a file using the browse button (this will lead to a file select dialog), things work fine!

 

BUT

 

When someone types in a rubbish value say “abcd” at the field beside this browse button (left), the page hangs! But I can get an error message, if I typed in rubbish value in the file select window which come after clicking the browse button.

 

(This happens for single or multiple conurrent uploads)

 

Can anyone help me to overcome this, thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/63912-concurrent-uploads/#findComment-376702
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.