Jump to content

Uploading an image gives me move_upload_file error


imgrooot
Go to solution Solved by imgrooot,

Recommended Posts

I am using a simple image upload. http://www.w3schools.com/php/php_file_upload.asp

 

It gives me 2 errors like this.

Warning: move_uploaded_file(C:/xampp/htdocs/home/upload/images/grandpix 2.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\home\upload\post.php on line 174
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpF915.tmp' to 'C:/xampp/htdocs/home/upload/images/grandpix 2.jpg' in C:\xampp\htdocs\home\upload\post.php on line 174

This is my code. Post.php .  I see that it's the "$target_dir" issue.  How can I fix it?

if(isset($_FILES['fileToUpload'])){
					
	if(!empty($_FILES['fileToUpload']['name'])) {
	
		$target_dir = $_SERVER['DOCUMENT_ROOT'].'/home/upload/images/';
		$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
		$uploadOk = 1;
		$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
		
		$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
		if($check !== false) {
			$uploadOk = 1;
		} else {
			$errors[] = 'File is not an image.';
			$uploadOk = 0;
		}
		
		// Check if file already exists
		if (file_exists($target_file)) {
			$errors[] = 'Sorry, file already exists.';
			$uploadOk = 0;
		}
		
		// Check file size
		if ($_FILES["fileToUpload"]["size"] > 500000) {
			$errors[] = 'Sorry, your file is too large.';
			$uploadOk = 0;
		}

		// Allow certain file formats
		if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
		&& $imageFileType != "gif" ) {
			$errors[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed.';
			$uploadOk = 0;
		}

		// Check if $uploadOk is set to 0 by an error
		if ($uploadOk == 0) {
			$errors[] = 'Sorry, your file was not uploaded.';
		// if everything is ok, try to upload file
		} else {
			if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
				echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
			} else {
				$errors[] = 'Sorry, there was an error uploading your file.';
			}
		}
		
		$insert_image = $db->prepare("INSERT INTO images(user_id, item_id, image_path, date_added) VALUES(:user_id, :item_id, :image_path, :date_added)");
		$insert_image->bindParam(':user_id', $userid);
		$insert_image->bindParam(':item_id', $item_id);
		$insert_image->bindParam(':image_path', $target_file);
		$insert_image->bindParam(':date_added', $date_added);
		if(!$insert_image->execute()) {
		
			$errors[] = 'There was a problem uploading the image!';
		
		} else {
		
			if(empty($errors)) {
				$db->commit();
				
				$success = 'Your post has been saved.';
			
			} else {
				$db->rollBack();
			}	
		
		}
				
	} else {

		$errors[] = 'An image is required!';
	}
} 
Edited by imgrooot
Link to comment
Share on other sites

Warning: move_uploaded_file(C:/xampp/htdocs/home/upload/images/grandpix 2.jpg): failed to open stream: No such file or directory

"No such file or directory"

 

Obviously it's not talking about the file - that isn't supposed to exist yet. So could it be that the directory does not exist?

Link to comment
Share on other sites

  • Solution

"No such file or directory"

 

Obviously it's not talking about the file - that isn't supposed to exist yet. So could it be that the directory does not exist?

 

 

Your comment made me think of something.  So I added this code and now it works.

if(!is_dir($target_dir)){
	mkdir($target_dir, 0775, true);
}
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.