Jump to content

help with file upload please


zipadee

Recommended Posts

Hi all,

 

On the website I'm creating, there's going to be a page with articles on. Basically what I want to do is allow my client to upload articles himself on a regular basis.

 

My PHP knowledge is very limited. However, using online tutorials etc. I've managed to create a user login. I've also created a form for uploading files to the server. This all (apparently) works fine.

 

I'm stuck now tho

 

How do I get the uploaded articles from the server back down into the actual articles page?

 

Ideally, I would like the articles' name and author (from the form) to be stored in a database. But I'm not sure if it's feasible to have the actual file that's being uploaded stored in the database too - I think the space I have is quite limited.

 

Thanks.

Link to comment
Share on other sites

example only, image from database.....

<?php session_start();

//database connection.

$sql="SELECT image_name FROM file WHERE id='$id'"; //$id assuming in session


$sql_result=mysql_query($sql) or die(mysql_error());


while($file=mysql_fetch_assoc($sql_result)){


echo "<img src='folder/".$file['image_name']." ' />";
}

?>

Link to comment
Share on other sites

thanks for your help.

 

This is getting the images from the database, isn't it? So how do I get the images to go to the database when the upload form is filled in? and if I wanted all the files to go into a table on my page, how would i go about doing that?

 

sorry, I'm probably asking completely dumb questions...

Link to comment
Share on other sites

very basic to show you the princable ok............

 

// create code to upload a pdf and put name in database .....
<?php session_start();

//database connection


// directory uploads for pdf file......
$uploaddir = '/uploads/';


// userfile name
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);


//put file in the directory as set

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    
echo "File is valid, and was successfully uploaded.\n";

// update database with file name

$sql="UPDATE database set pdf_name='".$_FILES['userfile']['name']."' WHERE id='$id'";

$sql_result=mysql_query($sql)or die(mysql_error);

} else {

    echo "Possible file upload attack!\n";
}

?>





//  create form post to it self...

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action=" " method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>


 

Link to comment
Share on other sites

please will you check I've done this right?

 

Here's my form:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

Article Title: <input type="text" name="title" id="title" />

Author: <input type="text" name="author" id="author" />

Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

<br />

<form name="logout" method="post" action="logout.php">
<input type="submit" value="Logout" />
</form>

</body>
</html>

 

and here's uploader.php:

<?php
ob_start();
$host="fdb1.awardspace.com"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="zipadee_oldhill"; // Database name
$tbl_name="articles"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$target_path = "articles/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['title']); 

if(move_uploaded_file($_FILES['uploadedfile']['title']['author'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['title']['author']). 
    " has been uploaded";	

$sql="UPDATE database set pdf_name='".$_FILES['title']['author']['uploadedfile']."' WHERE id='$id'";

$sql_result=mysql_query($sql)or die(mysql_error);

} else{
    echo "There was an error uploading the file, please try again!";
}
?>

 

I've created a table in my database called 'articles' and I have fields for 'ID', 'title', 'author' and file'. Is that what I need?

 

Thanks so much for your patience :)

 

Link to comment
Share on other sites

 

 

$_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif".

 

$_FILES['userfile']['size']

The size, in bytes, of the uploaded file.

 

 

 

 

dont be lazy now go back and read and apply what needed..........

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.