Jump to content

Help with PHP Upload Script


Fallen_angel

Recommended Posts

Hi ,


I am having a bit of trouble creatig n a script that will upload images to a database
via a form


I started off following the bellow tutorial

http://php.about.com/od/phpbasics/ss/mysql_files_4.htm


I have worked my way through it a few times now but I just can't work out where i am goign wrong

the form  I have in place is

[code]<form method="post" action="upload.php" enctype="multipart/form-data">
Name:<br>
<input type="text" name="filename" size="40">
<br>Description:<br>
<input type="text" name="s_description" size="40">
<br>Type:<br>
<input type="text" name="filetype" size="40">
<br>Size :<br>
<input type="text" name="filesize" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1024">
<br>File to upload:<br>
<input type="file" name="s_data" size="40"/>
<p><input type="submit" name="submit" value="submit">
</form>[/code]

and the processing page I have tried the following ( is named upload.php)

[code]<?php

include "connect.php";

$data = addslashes(fread(fopen($s_data, "r"), filesize($s_data)));
$result=MYSQL_QUERY("INSERT INTO strains_uploads (s_description,s_data,filename,filesize,filetype) ". "VALUES ('$s_description','$s_data','$filename','$filesize','$filetype')");
$id= mysql_insert_id();
print "<p>File ID: <b>$id</b><br>";
print "<p>File Name: <b>$filename</b><br>";
print "<p>File Size: <b>$filesize</b><br>";
print "<p>File Type: <b>$filetype</b><p>";
print "To upload another file <a href=http://www.yoursite.com/yourpage.html> Click Here</a>";
?> [/code]

The above enters the description ONLY , and returns
Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\Strainguide\upload.php on
line 4 at the top of the page bellow the file info area

after that I tried ( and currently still have inplace )


[code]<?php

$s_description=$_POST['s_description'];
$filesize=$_POST['filesize'];
$filename=$_POST['filename'];
$filetype=$_POST['filetype'];
$s_data=$_FILES['s_data'] ;

include "connect.php";

$data = addslashes(fread(fopen($s_data, "r"), filesize($s_data)));
$result=MYSQL_QUERY("INSERT INTO strains_uploads (s_description,s_data,filename,filesize,filetype) ". "VALUES ('$s_description','$s_data','$filename','$filesize','$filetype')");
$id= mysql_insert_id();
print "<p>File ID: <b>$id</b><br>";
print "<p>File Name: <b>$filename</b><br>";
print "<p>File Size: <b>$filesize</b><br>";
print "<p>File Type: <b>$filetype</b><p>";
print "To upload another file <a href=http://www.yoursite.com/yourpage.html> Click Here</a>";
?> [/code]

The above was a slight improvement as 90% of the info fills in as it should , however I still get the following 3 errors
Warning: fopen() expects parameter 1 to be string, array given in C:\wamp\www\Strainguide\upload.php on line 11

Warning: filesize() [function.filesize]: stat failed for Array in C:\wamp\www\Strainguide\upload.php on line 11

Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\Strainguide\upload.php on line 11

When I check the database I can see all the info there ( except in the s_data collum ) where i have [BLOB - 5 Bytes]
from my understanding this means that somethign is beign writen to the reccord  however a similar row usign the same
attachment that i added straight to the database through phpmyadmin says [BLOB - 41.1 KB] under it's data collum so I know
it's not placing in the image where it should

if someone could please help me figure out where i am goign wrong I would really apreciate the assitance

I am takign an educated  guess and assumign that it is somethign to do with the line in the uploads.php page that says  $data = addslashes(fread(fopen($s_data, "r"), filesize($s_data)));  however I am not sure what is wrong with that syntax

thanx to anyone that can help in advance


Link to comment
https://forums.phpfreaks.com/topic/25894-help-with-php-upload-script/
Share on other sites

You have to remember and if not remember realize that $_FILES['file'] is an array which contains the file name etc.
I would advise you to read up on that stuff before writing the script.

You've got quite a few issues here, for instance you're trying to put $s_data into the database which I don't think you wanted to do. What you might want to use for the fread or fopen is $_FILES['file']['tmp_name'] which is the uploaded file, but you should move the file first with move_uploaded_file () to an upload directory (check the function on PHP.net).

for instance:
[code]
$file_name = basename ($_FILES['file']['name']);
$directory = "/uploaddir/";
$uploaded_name = $directory.$file_name;
move_uploaded_file ($_FILES['file']['tmp_name'], $uploaded_name);
// now the file is in /uploaddir/file.ext and you can read it there
fread(...
[/code]
Hi , and thanx for your reply , I actually tried to use Post at first however that simply inserted nothing into the database
which is what i am trying to achive and what that tutorial i linked to was teaching me  , $s_data is definatly where i wanted it to go at the time of my first post ,

However in my searches for a solution I have come to find out that what i was planning was probably the wrong way to do things , and that  I should write a script that uploads to a directory and then have the database field referance that

Would you agree ?

thanx again for your help

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.