uploads
Posted: Sun Dec 18, 2005 9:38 am
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?
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;
}
?>