Simple File Upload going wrong... No such directory found?

I do not understand why this is happening. I’m finding the error:
Warning : move_uploaded_file(bulklister/bulklister-2018-12-17.csv): failed to open stream: No such file or directory in C:\xampp\htdocs\vrentals\inc\csv.inc.php on line 29

Whereas I used the same code for image uploading and it works just fine. Can anyone give me some guidelines on why my file is not uploading? Do you think it’s because I’m trying to upload a CSV and not an image?

if (isset($_POST['uploadCSV'])) {
	
 $today = date('Y-m-d');

	// code for file here
	$file = $_FILES['csvuploader'];
	$fileName = $_FILES['csvuploader']['name'];
	$fileTmpName = $_FILES['csvuploader']['tmp_name'];
	$fileSize = $_FILES['csvuploader']['size'];
	$fileType = $_FILES['csvuploader']['type'];
	$fileError = $_FILES['csvuploader']['error'];
	$fileExt = explode('.', $fileName);
	$fileActualExt = strtolower(end($fileExt));
	$allowed = array('csv');
	if (in_array($fileActualExt, $allowed)) {
		if ($fileError === 0) {
			if ($fileSize < 15000000) { /*15 MB*/
				$fileNameNew = "bulklister-".$today.".".$fileActualExt;
				$fileDestination = 'bulklister/'.$fileNameNew;
				/*Delete all files matching this filename including other file types.*/
				foreach(glob("bulklister/bulklister-{$today}.*") as $match) {
				    unlink($match);
				}
				move_uploaded_file($fileTmpName, $fileDestination);
				//$sqlUpload = "UPDATE modelimg SET status=0 WHERE modelid='$modelid';";
				//$resultUpload = mysqli_query($conn, $sqlUpload);
			}
		}
	}

Looks like the path is incorrect. When you do this with images, are the files in the same directory that this file is?

After working a couple hours on it I tried placing everything inside a function and then calling it when the upload button is clicked.

It worked :slight_smile:

I still have no idea why it wouldn’t work if I take it out of the function. I tried different variations like adding …/ and …/…/ and even re positioning all of the files. The only thing that made it work was wrapping it in a function and including the document inside the uploader doc. How does that work? :thinking::thinking::thinking:

Sponsor our Newsletter | Privacy Policy | Terms of Service