Cannot upload images with PHP

Hi there,

I am trying to allow visitors to upload images ( and later want to allow them to add audio files too so it would be nice to get comments on that as well :slight_smile: ) .
All the information gets uploaded correctly except the files. What could be the problem?
I came up with the following code:

	<form id="add-new-form" enctype="multipart/form-data">
		<input type="text" name="author" placeholder="author:" />
		<input type="text" name="title" placeholder="title:" />
		<textarea rows="10" cols="34" name="content" placeholder="content:"></textarea>
		<input type="file" name="image" />
		<input type="submit" id="add-new-button"  value="" />
	</form>
try {
	$conn = new PDO('mysql:host=localhost;dbname=brownie_manualforajump;', 'username', 'pass');
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
	echo 'ERROR: ' . $e->getMessage();
}

if(isset($_POST['author'])) {
	$author = htmlspecialchars($_POST['author']);
	$title = htmlspecialchars($_POST['title']);
	$content = htmlspecialchars($_POST['content']);

// properties of the uploaded file
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];

// if($error > 0) {
// 	die("Error uploading file! Code $error.");
// } else {
// 	if($type == "video/avi") { // conditions for the file
// 		die("That format is not allowed");
// 	} elseif ($size > 1048576) {
// 		die("File size cannot exceed 1mb");
// 	} else {
		move_uploaded_file($temp,"uploaded/".$name);
// 	}
// }
//

	$result = $author;

	echo json_encode($result);

	try {
		$stmt = $conn->prepare("INSERT INTO projects VALUES ('', :author, :title, :content, :path)");

		$stmt->bindParam('author', $author, PDO::PARAM_STR);
		$stmt->bindParam('title', $title, PDO::PARAM_STR);
		$stmt->bindParam('content', $content, PDO::PARAM_STR);
		$stmt->bindParam('path', $name, PDO::PARAM_STR);

		$stmt->execute();

	} catch(PDOException $e) {
		echo 'ERROR: ' . $e->getMessage();
	}
}

The first thing I would do is echo out these variables and see what they contain:


echo " name = $name <br> Type = $type <br> Size = $size  <br> Temp = $temp  <br> Error = $error ";

Quite often people are either trying to up load a folder rather than a file or the variable contains something else they are not expecting.

That’s the problem. All these values are absolutely empty while text input is fine. I have no idea what could be the problem and how to debug this :slight_smile:

It’s probably also worth mentioning that I have this code which is executed when ‘Submit’ button is pressed. It is AJAX so the page does not reload. Maybe that could be the problem

// Make a call to the server to retrieve all data
function retrieveResults(handleData) {
    return $.ajax({
        url: "forms-processing.php",
        type: 'POST',
        dataType: 'json',
        data: "projects=true",
        success: function(data) {
        	handleData(data);
        }
    });
}

// Add authors to contributors, menu item
retrieveResults(function(output){
	for(var i = 0; i<output.length; i++) {
		$('<a />', {
		    href: '#',
		    text: output[i].title
		}).wrap('<li />').parent().attr('data-author', output[i].author).appendTo('#project-titles');
		
		var position = $("#project-titles li:eq("+i+")").offset(); // calculate offset position of list items in terms of viewport
		var content = '\\
		<div data-author="' + output[i].author + '" style="left:' + position.left + 'px;top:' + (100 + Math.floor(Math.random()*500)) + 'px;z-index' + i + ';" class="project" id="project' + i + '">\\
			<h2>' + output[i].title + '</h2>\\
			<p>' + output[i].content + '</p>\\
		</div>';

		$('#content').append(content);
		$('#project' + i).draggable();
	}
	
	var listCount = $("#project-titles li").size();
	$('.project').on('click', function() {
		listCount += 1;
		$(this).css('z-index', listCount);
	});
});

Ok I figured it out. File upload does not work with AJAX, that is the problem.