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
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')";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.