Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, today I was watching a tutorial on how to upload images using php and working on it when I stumbled upon an issue. My code was working, the success header was shown, however the actual file was not uploading to the desired folder. I checked the spelling and destination of the folder multiple times and have switched the destination of the file to my rootfolder and still no image was upoaded. I am not at the point of inserting my data into the database yet. Below is my php code and here is the link of the youtube tutorial
mmtuts File upload tutorial

PHP
<?php

if(isset($_POST['submit'])){ //check if submit button with name submit pressed//
	$file = $_FILES['file']; //save file uploaded from [input type= file] with name file//

	$fileName = $_FILES['file']['name']; //getting file uploaded name from inline array//
	$fileTmpName = $_FILES['file']['tmp_name']; //getting file temporary name from inline area recieved form print_r() f//
	$fileSize = $_FILES['file']['size']; //getting file size form inline area recieved from print_r() f//
	$fileError = $_FILES['file']['error']; //getting file upload error from inline array//
	$fileType = $_FILES['file']['type']; //getting uploaded file type//

	$title = $_POST['title']; //getting title for photo from index.php title input//
	$desc = $_POST['desc']; //getting image description from index.php desc input//

	$fileExt = explode('.', $fileName); //removeing all '.' from fileName//
	$fileActExt = strtolower(end($fileExt)); //putting the type of file in lower case by only using the end letters of file and functioning to strtolower//

	$allowed = array('jpg', 'jpeg', 'png'); //in array setting types of allowed files//

	if(in_array($fileActExt , $allowed)){
		if($fileError === 0){
			if($fileSize < 20000000){
				$fileNameNew = uniqid('', true). '.' .$fileActExt;
				$fileDest = 'rootfolder/' . $fileNameNew;
				move_uploaded_file($fileTmpName, $fileDest);
				header("Location: index.php?upload=success");

			}else{
				echo "File to large";
			}


		}else{
			echo "error!";
		}

	}else{
		echo "Unaccepted file type";
	}



}


What I have tried:

I have tried switching the destination, checking and double checking the destination. I have even tried putting the file into my root folder.
Posted
Updated 12-Sep-18 3:27am

1 solution

file.php

<html>
<form action="upload.php" method="POST" enctype='multipart/form-data'>
	<input type="file" name="file">
	<input type="submit" value="save" name="submit">
</form>
</html>


upload.php

<?php
if(isset($_POST['submit'])){
// print_r($_FILES);exit; //check if submit button with name submit pressed//
	$file = $_FILES['file']; //save file uploaded from [input type= file] with name file//

	$fileName = $_FILES['file']['name']; //getting file uploaded name from inline array//
	$fileTmpName = $_FILES['file']['tmp_name']; //getting file temporary name from inline area recieved form print_r() f//
	$fileSize = $_FILES['file']['size']; //getting file size form inline area recieved from print_r() f//
	$fileError = $_FILES['file']['error']; //getting file upload error from inline array//
	$fileType = $_FILES['file']['type']; //getting uploaded file type//

	$title = $_POST['title']; //getting title for photo from index.php title input//
	$desc = $_POST['desc']; //getting image description from index.php desc input//

	$fileExt = explode('.', $fileName); //removeing all '.' from fileName//
	$fileActExt = strtolower(end($fileExt)); //putting the type of file in lower case by only using the end letters of file and functioning to strtolower//

	$allowed = array('jpg', 'jpeg', 'png'); //in array setting types of allowed files//

	if(in_array($fileActExt , $allowed)){
		if($fileError === 0){
			if($fileSize < 20000000){
				$fileNameNew = uniqid('', true). '.' .$fileActExt;
				$fileDest = 'rootfolder/' . $fileNameNew;
				move_uploaded_file($fileTmpName, $fileDest);
				header("Location: index.php?upload=success");

			}else{
				echo "File to large";
			}


		}else{
			echo "error!";
		}

	}else{
		echo "Unaccepted file type";
	}

}


Please check this.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900