Jump to content

uploading a file


54uydf

Recommended Posts

Hi, really need some extra brains to help me with this one!

I have a form with only 1 text box (input type=text)

in this textbox I enter a local path on my computer to a specific folder. in this folder I have few files, each has a set name that I know in advance.

what I want to do is to be able to upload all those files in my local folder to the server, without making the user click browes and pick them 1 by 1.

I searched and searched but I see it's not as simple as I thought cuz I can't just set the value of the input file..

since my page has a form eitherway I thought of maybe doing something sneaky and hiding the input files and some how passing them the path of each file (the folder path will be taken from the not hidden textbox) and the file name is known to me so I can add those 2 somehow..

I can't find a way to do that, so I wonder if any of you have an idea? maybe it's possible to set the file path after the form is submited? basicly I think it's possible to give the name of the file, but I also need the temp name of the file ($_FILES['docfile1']['tmp_name']), and this I don't know how to set..

 

this is my code for uploading a file with <input type=file>

if ($_FILES['docfile1']['name']!='') {
$target = 'import/';    
$target = $target . basename($_FILES['docfile1']['name']);
if ($target =="import/myfile.xlsx"){
	if (file_exists($target))
		unlink($target);
	if (move_uploaded_file($_FILES['docfile1']['tmp_name'], $target))   {			
                                                echo 'success';
	}else{
		echo 'fail';  		
	}
}else{
         echo 'fail';
}
}

desperately need help!

Link to comment
Share on other sites

It's impossible for you to use a browser to upload preselected files from a client as that would allow any web site to grab any file from a client's system. The safeguards built in require user action to select the actual file(s).

 

You can 'filter' which files are listed in the file selection dialog box and allow multiple files to be selected at one time, by using a flash form. See this link - http://swfupload.org/

 

Also, in your existing code, $_FILES['docfile1']['name']!='' doesn't insure that the file was successfully uploaded. Some of the upload errors will set the ['name'] element, but the file upload failed. You need to test the value in the ['error'] element and only use the uploaded file information if the error was zero. See this link - http://us2.php.net/manual/en/features.file-upload.errors.php

Link to comment
Share on other sites

i tryed that once and could not work it out

instead i made a form where up to 10 files can be uploaded

<?php
session_start();
require('connection');
$myusername=$_SESSION['myusername'];
$mypassword=$_SESSION["mypassword"];
$ivn=$_SESSION["ivn"];


if (($_FILES['uploadedfile']['size']!= 0 && $_FILES['uploadedfile1']['size']!= 0 && $_FILES['uploadedfile2']['size']!= 0 && $_FILES['uploadedfile3']['size']!= 0 && $_FILES['uploadedfile4']['size']!= 0 && $_FILES['uploadedfile5']['size']!= 0 && $_FILES['uploadedfile6']['size']!= 0  && $_FILES['uploadedfile7']['size']!= 0 && $_FILES['uploadedfile8']['size']!= 0 && $_FILES['uploadedfile9']['size']!= 0 ))

{


$ext_a = explode(".", $_FILES['uploadedfile']['name']);
$ext_a1 = explode(".", $_FILES['uploadedfile1']['name']);
$ext_a2 = explode(".", $_FILES['uploadedfile2']['name']);
$ext_a3 = explode(".", $_FILES['uploadedfile3']['name']);
$ext_a4 = explode(".", $_FILES['uploadedfile4']['name']);
$ext_a5 = explode(".", $_FILES['uploadedfile5']['name']);
$ext_a6= explode(".", $_FILES['uploadedfile6']['name']);
$ext_a7 = explode(".", $_FILES['uploadedfile7']['name']);
$ext_a8 = explode(".", $_FILES['uploadedfile8']['name']);
$ext_a9 = explode(".", $_FILES['uploadedfile9']['name']);

$ext = $ext_a[1];
$ext2 = $ext_a1[1];
$ext3 = $ext_a2[1];
$ext4 = $ext_a3[1];
$ext5 = $ext_a4[1];
$ext6 = $ext_a5[1];
$ext7 = $ext_a6[1];
$ext8 = $ext_a7[1];
$ext9 = $ext_a8[1];
$ext10 = $ext_a9[1];






if ( ($ext!=="gif" && $ext!=="JPG" && $ext!=="png" && $ext!=="jpg" )){
echo "Images Must Be JPG,PNG, OR GIF";
echo "<a href='port2.php'>Go back</a>";
die;
}

/* hashes the file name with the date and time and rand number to genrate a uniue file name*/
$ran=mt_rand(1000, 9999);
$ran1=mt_rand(1000, 9999);
$ran2=mt_rand(1000, 9999);
$ran3=mt_rand(1000, 9999);
$ran4=mt_rand(1000, 9999);
$ran5=mt_rand(1000, 9999);
$ran6=mt_rand(1000, 9999);
$ran7=mt_rand(1000, 9999);
$ran8=mt_rand(1000, 9999);
$ran9=mt_rand(1000, 9999);

$img=md5($_FILES['uploadedfile']['name'] . date("m.d.y H:m:s"))   .$ran. "." . $ext;
$img2=md5($_FILES['uploadedfile1']['name'] . date("m.d.y H:m:s")) .$ran1. "." . $ext2;
$img3=md5($_FILES['uploadedfile2']['name'] . date("m.d.y H:m:s")) .$ran2."." . $ext3;
$img4=md5($_FILES['uploadedfile3']['name'] . date("m.d.y H:m:s")) .$ran3."." . $ext4;
$img5=md5($_FILES['uploadedfile4']['name'] . date("m.d.y H:m:s")) .$ran. "." . $ext5;
$img6=md5($_FILES['uploadedfile5']['name'] . date("m.d.y H:m:s")) .$ran5. "." . $ext6;
$img7=md5($_FILES['uploadedfile6']['name'] . date("m.d.y H:m:s")) . $ran6."." . $ext7;
$img8=md5($_FILES['uploadedfile7']['name'] . date("m.d.y H:m:s")) . $ran7."." . $ext8;
$img9=md5($_FILES['uploadedfile8']['name'] . date("m.d.y H:m:s")) . $ran8."." . $ext9;
$img10=md5($_FILES['uploadedfile9']['name'] . date("m.d.y H:m:s")) . $ran9."." . $ext10;




$target_path = "../portimg/";
$target_path1 = "../portimg/";
$target_path2 = "../portimg/";
$target_path3 = "../portimg/";
$target_path4 = "../portimg/";
$target_path5 = "../portimg/";
$target_path6 = "../portimg/";
$target_path7 = "../portimg/";
$target_path8 = "../portimg/";
$target_path9 = "../portimg/";

$target_path = $target_path .basename($img) ; 
$target_path1 = $target_path1 .basename($img2);
$target_path2 = $target_path2 .basename($img3);
$target_path3 = $target_path3 .basename($img4); 
$target_path4 = $target_path4 .basename($img5);
$target_path5 = $target_path5 .basename($img6);
$target_path6 = $target_path6 .basename($img7); 
$target_path7 = $target_path7 .basename($img8);
$target_path8 = $target_path8 .basename($img9);
$target_path9 = $target_path9 .basename($img10);



$result = mysql_query("UPDATE members SET p1='$img', p2='$img2', p3='$img3',p4='$img4', p5='$img5', p6='$img6',p7='$img7', p8='$img8', p9='$img9',p10='$img10' WHERE email='$myusername' and password='$mypassword' and inv='$ivn';") 
or die(mysql_error());



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3designbrief.com | Create Porfolio</title>
<link href="../css/3lance.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="content" style="background-image:url(../img/3lance.png);">
<div class="slideport">
<?php
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
}
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1)){
}
if(move_uploaded_file($_FILES['uploadedfile2']['tmp_name'], $target_path2)){
}
if(move_uploaded_file($_FILES['uploadedfile3']['tmp_name'], $target_path3)) {
}
if(move_uploaded_file($_FILES['uploadedfile4']['tmp_name'], $target_path4)){
}
if(move_uploaded_file($_FILES['uploadedfile5']['tmp_name'], $target_path5)){
}
if(move_uploaded_file($_FILES['uploadedfile6']['tmp_name'], $target_path6)) {
}
if(move_uploaded_file($_FILES['uploadedfile7']['tmp_name'], $target_path7)){
}
if(move_uploaded_file($_FILES['uploadedfile8']['tmp_name'], $target_path8)){
}
if(move_uploaded_file($_FILES['uploadedfile9']['tmp_name'], $target_path9)){

echo "Profile Image has been uploaded";

 

 

 

Link to comment
Share on other sites

conan, from from first look of ur code, I don't see where is the form that let's the user choose the files..am I missing it?

I'll try ur code a bit later. thank you!

 

I'm trying to figure out how to use the SWFUpload for the last 2 hours.. so great but I'll have less problems making a hole in the wall with my head! >:(

 

I hope I'll figure it out. it was the only solution that allowed to select few files at once, if there are others that are simpler to understand and use I'd gladly try those

Link to comment
Share on other sites

 

here is the form

<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="../php/goldportupload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>multiple Files Upload </strong></td>
</tr>
<tr>
<td>Select file 
<input name="uploadedfile" type="file" id="uploadedfile" size="20" />

<td>Select file
<input name="uploadedfile1" type="file" id="uploadedfile1" size="20" /></td>
</tr>
<tr>
<td>Select file
<input name="uploadedfile2" type="file" id="uploadedfile2" size="20" />

<td>Select file 
<input name="uploadedfile3" type="file" id="uploadedfile3" size="20" /></td>
</tr>
<tr>
<td>Select file
<input name="uploadedfile4" type="file" id="uploadedfile4" size="20" />

<td>Select file
<input name="uploadedfile5" type="file" id="uploadedfile5" size="20" /></td>
</tr>
<tr>

<td>Select file 
<input name="uploadedfile6" type="file" id="uploadedfile6" size="20" />

<td>Select file
<input name="uploadedfile7" type="file" id="uploadedfile7" size="20" /></td>
</tr>
<tr>

<td>Select file
<input name="uploadedfile8" type="file" id="uploadedfile8" size="20" />

<td>Select file
<input name="uploadedfile9" type="file" id="uploadedfile9" size="20" /></td>
</tr>

</table>
</div>
<div class="uploadbutton" style="background-color:#A0D9F8;";>
<input type="submit" name="Submit" value="Upload" />
</form>
<form action="port3.php">
<input type="submit" value="uploadlater" />

</form>

Link to comment
Share on other sites

conan318, you wouldn't write out all that code, repeated for each form field. You would use an array name for the form field and use a simple loop to iterated over the uploaded file information.

 

that would be a smarter way to do it. i am still learning php. :) next time i will try it that way

Link to comment
Share on other sites

conan, that's exactly what I don't want to have, the 10+ <input type="file" > lol

nevermind, thank you anyways :)

 

anybody here ever worked with SWFUpload before??

I got it to upload my files, they show up at the targeted folder but it says that the upload has failed (error 500)

I'm using XAMMP so I wonder if the files move cause both folders (from and to) are on my computer and on real server this won't work? any idea?

Link to comment
Share on other sites

If you want help with your current code and the error, you would need to post enough of the form and the processing code that duplicates the error. I'm guessing the 500 error is actually a HTTP 500 response code, which doesn't mean that the upload failed, it means that the server returned an incomplete HTTP response.

Link to comment
Share on other sites

well I'll start by saying that I downloaded the SWFUpload_v2.2.0.1_Samples from the SWFUpload site

I moved the css, images,js,swfupload folders to my xampp folder, and opened another folder in it called uploading. (so it's 5 folders)

in folder uploading I have a folder uploadedFiles, to where I want the files to be uploaded to, also I have a php file that has the SWFUpload  form that allows to choose the files (import.php), and another php file where I added my code that saves the files (upload.php) , since SWFUpload doesn't provide it..

here are the 2 php files:

the one with the form (import.php):

<?php
session_start();

if (count($_FILES)) {
        // Handle degraded form uploads here.  Degraded form uploads are POSTed to index.php.  SWFUpload uploads
	// are POSTed to upload.php
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<link href="../css/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/swfupload.queue.js"></script>
<script type="text/javascript" src="js/fileprogress.js"></script>
<script type="text/javascript" src="js/handlers.js"></script>
<script type="text/javascript">
	var upload2;
	window.onload = function() {
		upload2 = new SWFUpload({
			// Backend Settings
			upload_url: "upload.php",
			post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},

			// File Upload Settings
			file_size_limit : "4096 ",	// 4 MB
			file_types : "*.*",
			file_types_description : "All Files",
			file_upload_limit : "10",
			file_queue_limit : "5",

			// Event Handler Settings (all my handlers are in the Handler.js file)
			file_dialog_start_handler : fileDialogStart,
			file_queued_handler : fileQueued,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,

			// Button Settings
			button_image_url : "XPButtonUploadText_61x22.png",
			button_placeholder_id : "spanButtonPlaceholder2",
			button_width: 61,
			button_height: 22,

			// Flash Settings
			flash_url : "../swfupload/swfupload.swf",

			swfupload_element_id : "flashUI2",		// Setting from graceful degradation plugin
			degraded_element_id : "degradedUI2",	// Setting from graceful degradation plugin

			custom_settings : {
				progressTarget : "fsUploadProgress2",
				cancelButtonId : "btnCancel2"
			},

			// Debug Settings
			debug: false
		});

     }
</script>
</head>
<body>

<div id="content">

<form id="form1" action="upload.php" method="post" enctype="multipart/form-data">

	<div>
		<div class="fieldset flash" id="fsUploadProgress2">
			<span class="legend">Small File Upload Site</span>
		</div>
		<div style="padding-left: 5px;">
			<span id="spanButtonPlaceholder2"></span>
			<input id="btnCancel2" type="button" value="Cancel Uploads" onclick="cancelQueue(upload2);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
			<br />
		</div>
	</div>

</form>
</div>
</body>
</html>

 

and the one that saves the files (upload.php):

 

<?php
if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["PHPSESSID"]);
}
session_start();
if ($_FILES['Filedata']['name']!='') {
	$target = 'uploadedFiles/';    
	$target = $target . basename($_FILES['Filedata']['name']);
	move_uploaded_file($_FILES['Filedata']['tmp_name'], $target);		
}
// The Demos don't save files
// In this demo we trigger the uploadError event in SWFUpload by returning a status code other than 200 (which is the default returned by PHP)
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
	// Usually we'll only get an invalid upload if our PHP.INI upload sizes are smaller than the size of the file we allowed
	// to be uploaded.
	header("HTTP/1.1 500 File Upload Error");
	if (isset($_FILES["Filedata"])) {
		echo $_FILES["Filedata"]["error"];
	}
	exit(0);
}
?>

the only thing I changed in this file is I added this code:

if ($_FILES['Filedata']['name']!='') {
$target = 'uploadedFiles/';    
$target = $target . basename($_FILES['Filedata']['name']);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $target);		
}

Link to comment
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.