Jump to content

File upload form not working correctly


PHPiSean

Recommended Posts

I've looked over it a couple times and I still get the error. Here's the code

<?php
require('header.php');
require('links.php');

$name = mysql_real_escape_string($_POST['file']);
$url = mysql_real_escape_string($_POST['file']);

$filename = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];

$sql = "insert into books set name='$name', url='$url'";

if(isset($_POST['submit'])) {
if($error > 0) {
	die("Error uploading file! Code $error.");
}else{
	move_uploaded_file($temp,"/center/resources/books/".$filename);
	mysql_query($sql);
}
}
?>

<form method='post' enctype='multipart/form-data'>
Book Name<input type="text" name="name"></br>
File<input type="file" name="file"></br>
<input type="submit" name="submit" value="Upload">
</form>

Link to comment
https://forums.phpfreaks.com/topic/228077-file-upload-form-not-working-correctly/
Share on other sites

It helps if you actually state what error you're getting.

 

With that said, this is obviously wrong:

 

$sql = "insert into books set name='$name', url='$url'";

 

An insert statement format that might actually work:

 

$sql = "insert into books (name, url) values ('$name', '$url')";

I have also found errors given by the browser

 

Warning: move_uploaded_file(/center/resources/books/sources.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move

Well, the longer I work with Mysql the less it surprises me when they cook up some crazy non-standard syntax. 

 

The error you're having is definately a path or permissions issue.  Does the path:  /center/resources/books/ actually exist?  This can't be a relative path, it has to be a real path on the server.  It will not make directories for you either -- they have to exist or the move_uploaded_file fails.  And as already suggested by codingPhoenix, the directory has to have rwx perms that apply to the apache process as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.