uploads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

uploads

Post by ale2121 »

Hi,
I have a question about uploading files using a form. I've pasted my code below which uploads the files just fine, but when I try to access the uploaded files, using just a direct path to the file, it can't be viewed. For example. If I upload an image or an mp3, then try type the url of the image, I get a page cannot be found error. If I upload or download these same files through fetch, they work just fine. Any thoughts?

Code: Select all

<?php 
session_start();
if ($_SESSION['username']) {
$page_title = 'Upload Files';
require_once ('mysql_connect.php');
$message = NULL;
if (isset($_POST['submit'])) {
	if (isset($_FILES['upload']['name'])) {
		$u = TRUE;
	} else {
		$u = FALSE;
		$message .= '<p>Please select a file to upload!</p>';
	}
	if ($u) {
		// slashes and stuff
		function escape_data($data) {
			global $dbc;
			if (ini_get('magic_quotes_gpc')) {
				$data = stripslashes($data);
			}
			return mysql_real_escape_string (trim ($data), $dbc);
		}
		//upload file1
		$extension = explode('.',$_FILES['upload']['name']);
			$filename = rand(1,1000000).'.'. $extension[1];
			while (file_exists($filename)) {
					$filename = rand(1,1000000).'.'.$extension[1];
			}
		$query = "INSERT INTO soundfiles (file_name, file_size, file_type, upname) VALUES ('{$_FILES['upload']['name']}','{$_FILES['upload']['size']}','{$_FILES['upload']['type']}','$filename')";
		$result = @mysql_query ($query);
		if ($result) {
			if (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/$filename")) {
				$numFiles = $numFiles + 1;
			} 
?>
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

The destination of move_uploaded_file() doesn't look right: uploads/$filename
Is that in the document root of the web server? Maybe that's why you can't veiw it?
ale2121
Forum Newbie
Posts: 23
Joined: Tue Aug 23, 2005 9:56 am

Post by ale2121 »

uploads is in my root folder.
If I use fetch to upload files to this folder, I can view those files, just not the ones uploaded through my form.
:?

also, I know its uploading the mime type correctly because its showing up in my database correctly.
Post Reply