Hi, if I have few files (xlsx) in a folder on my computer (file's name is known), and I want to give the path of the folder in my form using a textbox,

<input type="text" name="path" id="path" >

and for the files to be uploaded to a specific folder on the server so I can use them with php, how can I do that?
I know how to upload files with <input type="file"..>
this is my code for uploading a single file like that:

if ($_FILES['docfile1']['name']!='') {
	$target = 'importFiles/';    
	$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';
	}
}

I'm trying to change this code so that I can use the path of the folder instead of the input file, but how can I get the $_FILES? I can't figure it out..

Dani AI

Generated

Short answer: you cannot make the server read files from a visitor's PC just by posting a local folder path in a text box. Browsers will not expose arbitrary client files or allow a page to fetch "C:..." or "/Users/..." without an explicit user action (file selection, drag‑and‑drop, or an approved picker). This is by design in the HTML spec to prevent sites from reading a visitor's disk. HTML File upload state. (html.spec.whatwg.org)

If the XLSX files are already on the server, PHP can read them by path with normal file functions (for example file_get_contents, fopen, etc.). If the files are on the client machine you must have the user select and upload them. Practical options:

  • Let users pick files (multiple) with a file input.
  • Let users pick a folder (where supported) using the nonstandard webkitdirectory attribute.
  • Use the modern File System Access API (showDirectoryPicker) for richer directory access in supporting browsers.

Example form (folder selection where supported):

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" id="files" name="files[]" multiple webkitdirectory />
<button type="submit">Upload</button>
</form>

Client-side you can inspect event.target.files and file.webkitRelativePath to preserve subfolders; server-side handle $_FILES['files'] in a loop and call move_uploaded_file() for each item. See webkitdirectory and server upload handling for details. webkitdirectory. move_uploaded_file. (developer.mozilla.org)

For a better UX than plain file inputs, AJAX upload libraries (what hinted at) just POST the files selected by the user — they do not bypass the browser sandbox. The File System Access API (showDirectoryPicker) is another route but requires HTTPS and browser support; use it only where supported. showDirectoryPicker. (developer.mozilla.org)

I think Ajax upload might do the job. Actually I haven't tried it. Check this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.