Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,172,997 members, 7,886,724 topics. Date: Thursday, 11 July 2024 at 02:21 PM

Php Script Giving Me Headache: Help! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Php Script Giving Me Headache: Help! (2127 Views)

HELP - Installing A PHP Script On My Website Using GITHUB / Can Obfuscated Php Script Be Decoded? / I Need Help With A PHP Script (2) (3) (4)

(1) (Reply) (Go Down)

Php Script Giving Me Headache: Help! by face2tu: 2:39pm On Sep 24, 2011
Hi nairalanders, please I have being working on a media site for few months now and I have designed everything that needs to be designed but A♏ having problems with my uploading code its not storing the file size in the database, I have tried and tried and all what have being doing seem to be taking me away from the right way with my little understanding of php. Here is the code that is giving me headache


,
<?php //include("includes/sess.php"wink; ?>
<?php
$upload="active";
include("includes/mheader.php"wink; ?>
<?php
if(isset($_POST['Upload'])) {
//////////////////////////////////
//$id=$_POST['id'];
$picture=$_POST['picture'];
$artiste=$_POST['artiste'];
$title=$_POST['title'];
$genre=$_POST['genre'];
$uname=$_POST['uname'];




if($title==""wink
{
$message="unknown Title";
header("location:upload.php?message=$message"wink;
exit();
}

if($genre==""wink
{
$message="unknown Type";
header("location:upload.php?message=$message"wink;
exit();
}

/*if($nfile==""wink
{
$message="file not found";
header("location:upload.php?message=$message"wink;
exit();
} */



if($uname==""wink
{
$message="error you";
header("location:upload.php?message=$message"wink;
exit();
}

if($artiste==""wink
{
$message="Artiste ?";
header("location:upload.php?message=$message"wink;
exit();
}

header("location:upload.php?message=$message"wink;

//////////////////////////////////
$size=$_FILES["picture"]["size"];
$file_size1 = $size/1024 ;
$file_size = $file_size1/1000 ;
function UploadOne($fname)
{
$uploaddir = 'music/NaijaMp3_';
if (is_uploaded_file($fname['tmp_name']))
{
$filname = basename($fname['name']);
$uploadfile = $uploaddir . basename($fname['name']);
if (move_uploaded_file ($fname['tmp_name'], $uploadfile))
$res = "File " . $filname . " was successfully uploaded and stored.<br>";
else
$res = "Could not move ".$fname['tmp_name']." to ".$uploadfile."<br>";
}
else
$res = "File ".$fname['name']." failed to upload.";
return ($res);
}
?>
<?php
if ($_FILES['picture']['name'] != ""wink
{
$res = UploadOne($_FILES['picture']);
$filname = $_FILES['picture']['name'];
$filname=addslashes($filname);
/////////////////////////////////////////////////////////////
$alt_file_name = "NaijaMp3_" .$filname ;

$date_created=date("Y-m-d h:i:A"wink;
include("includes/db_connect.php"wink;
$sql="INSERT INTO upmp3(uname,file_name,alt_file_name,file_size,artiste,title,genre,date_created)
VALUES('$uname','$filname','$alt_file_name','$file_size','$artiste','$title','$genre','$date_created')";
mysql_query($sql) or die (mysql_error());
$message="Upload Sucessful";
/////////////////////////////////////////////////////////////

echo ($res);
}
}
?>




<div class="main_but">

</div>





</div>

<!-- Navigation begins here -->
<!-- Header section ends here --></div>
<div class="login">
</div>
<div id="feature">
<blockquote/>
<div align="center"><em><strong>UPLOAD FILE </strong></em></div>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>



</p>
<p>{Note: All * fields are Mandatory} </p>
<p>
<label>
<input name="picture" type="file" id="picture" />
</label>
</p>
<p>File Type
<label>
<select name="type" id="type">
<option value="mp3">mp3</option>
</select>
</label>
</p>
<p>Artiste*<span class="style1"><sub>(e.g Alashine)</sub></span>
<label>
<input name="artiste" type="text" id="artiste" />
</label>
Title

<label>
<input name="title" type="text" id="title" /><span class="style1">
</label>
<p>Genre
<label><input name="genre" type="text" id="genre" />(Genre e.g Hiphop, RnB, Reggae . . .)</span></label>
<input type='hidden' name='id' value='<?php echo"$id" ?>' id='id'/>
<input type='hidden' name='uname' value='<?php echo"$uname" ?>' id='uname'/>
<label></label>
</p>


</p>
<p>
<label>
<input name="Upload" type="submit" id="Upload" value="Upload Mp3" />
</label>
</p>
</form>
</div>
</div>
<?php include("includes/footer.php"wink; ?>
</div>


Thanks for your help in anticipation
Re: Php Script Giving Me Headache: Help! by moderatorr: 8:11pm On Sep 24, 2011
thank you too.
Re: Php Script Giving Me Headache: Help! by Fayimora(m): 10:29pm On Sep 24, 2011
Would have loved to read your stuff but its really messy, just post the part that has to do with uploading and leave every other thing out, Also, make sure you indent your code. Paste your code here -> pastebin.com and share the link. Thats how you are going to get actual help!
Re: Php Script Giving Me Headache: Help! by worldbest(m): 12:34am On Sep 25, 2011
http://pastebin.com/ru3QzmR3 - Here is a pastebin link to his source code
Re: Php Script Giving Me Headache: Help! by wassolldas: 2:41am On Sep 25, 2011
You can try this and see if it makes a difference


<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input name="picture" type="file" id="picture" />


Reference: http://www.php.net/manual/en/features.file-upload.post-method.php
Re: Php Script Giving Me Headache: Help! by Fayimora(m): 10:46am On Sep 25, 2011
worldbest:

http://pastebin.com/ru3QzmR3 - Here is a pastebin link to his source code

lol doesnt make a difference. He should indent and post only relevant snippets, Anyways, you can start by echoing mysql_error() under ur query so u are sure you dnt ave problems with sql,
Re: Php Script Giving Me Headache: Help! by bakenda(m): 9:23am On Sep 26, 2011
Av studied the script, didn't seem to find anything
that should make it not to work, is it only the file
size that's not getting inserted or other fields as well?

Can see you already av a die (mysql_error()) there,
what final error message do you get?
Re: Php Script Giving Me Headache: Help! by farouqzaib: 4:56pm On Sep 26, 2011
@OP,
Didn't have time to test ya code(no vex!).
OK! Need to "troubleshoot " your code with you.
1. Does the code upload the file to the upload folder?
2. Does a record get inserted for any file upload?
3. If yes to above;
3a. Make sure your filesize column is of the correct data type(String(varchar n d rest) should accept numbers. It doesn't hurt to switch it.)
3b. If no, try echoing out all the variables to be used for the DB insert.
Re: Php Script Giving Me Headache: Help! by face2tu: 5:41pm On Sep 26, 2011
@bakenda: its just the file_size that's giving me headache,

@farouqzaib:
1. Do I have to create an upload folder in the root directory?

All the other records are stored in the database but the file_size always keep inserting "0" there and not picking the real file size of the file.
Re: Php Script Giving Me Headache: Help! by bakenda(m): 4:01am On Sep 27, 2011
What data type did you set for the file_size column?
Like farouq said you may need to change it.
Let's hear how it goes.
Re: Php Script Giving Me Headache: Help! by Mobinga: 8:09pm On Sep 27, 2011
Yo' how many times are you dividing the image size?


<?php
$size=$_FILES["picture"]["size"];
$file_size1 = $size/1024 ;
$file_size = $file_size1/1000;
?>


First ::  Remove the second division. You have to divide it by 1024, to get the size in KB. Since its an image the size would be 5MB MAX, so why are you dividing it by 1000*1000?

Secondly, what data type is the column accepting? You must have set it to an unsuitable data type.

Finally, your code is vulnerable to SQL injection.

See http://www.php.net/manual/en/function.mysql-real-escape-string.php  and http://www.google.com.ng/url?sa=t&source=web&cd=1&ved=0CCsQFjAA&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSQL_injection&ei=Ph-CTrLEB8vO4QT6vMFP&usg=AFQjCNGcGZNbIk2jM8itxItHrCja7eJ0PQ
Re: Php Script Giving Me Headache: Help! by face2tu: 10:59am On Sep 28, 2011
Was using Varchar(111), it didn't work, changed it to int(111) still not working, A friend of mine said I should check my php.ini and increase the upload size to 80M but it is still not working, Please which Nairalanders PHP Masters stay in Lagos? I will come with my PC.
Re: Php Script Giving Me Headache: Help! by Mobinga: 12:13pm On Sep 28, 2011
face2tu:

Was using Varchar(111), it didn't work, changed it to int(111) still not working,  A friend of mine said I should check my php.ini and increase the upload size to 80M but it is still not working,  Please which Nairalanders PHP Masters stay in Lagos? I will come with my PC.

Have you tried saving the raw size to your database? Have tried removing the 1000*1000 division your doing? I've made a revision to your code, http://pastebin.com/k8kHhHSe, when you run it, go to your script location / size.txt open it and tell us the content.
Re: Php Script Giving Me Headache: Help! by face2tu: 4:59pm On Sep 28, 2011
@mobinga: thank you very much Sir and everybody that has helped me in finding solution to this headache giving problem, Words can't explain how happy I am to have gotten a solution to this code.

Please what do you mean by my code is vulnerable to sql injection while I also read the references you made available.
Re: Php Script Giving Me Headache: Help! by Mobinga: 7:42pm On Sep 28, 2011
face2tu:

@mobinga: thank you very much Sir and everybody that has helped me in finding solution to this headache giving problem, Words can't explain how happy I am to have gotten a solution to this code.

Please what do you mean by my code is vulnerable to sql injection while I also read the references you made available.

Your welcome, what was the solution?

About the SQL injection, before you send any data to the database, you have to sanitize it.
Check out how it works http://en.wikipedia.org/wiki/SQL_injection
Re: Php Script Giving Me Headache: Help! by face2tu: 12:22pm On Sep 30, 2011
I had to remove the other division of the file size. The file_size1 and I had to change my php.ini upload_file_max to 80M. That was all I did. But now am another major problem o! My download script is not downloading the file itself, its downloading the php file but not the file uploaded in mp3.


<html>

<head>

<title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<?php

include("includes/db_connect.php"wink;

$query="SELECT id,file_name FROM upmp3";

$result= mysql_query($query) or die('Error query failed');

if(mysql_num_rows($result) == 0){

echo"File no longer in database";

}else{

while(list($id,$title) = mysql_fetch_array($result)){

echo"<a href=\"get_mp3.php?id=$id\">Download $title</a><br>";

}

}

 

?>

<?php

include("includes/db_connect.php"wink;

if(isset($_GET['id'])){

//if id is set then get data from database

$id = $_GET['id'];

$query="SELECT file_name,alt_file_name,file_size,artiste,title,genre FROM upmp3 WHERE id=$id";

$result= mysql_query($query) or die('Error ID not selected');

 

header("Content-lenght: $file_size"wink;

header("Content-type: music/mp3"wink;

header("Content-Disposition: attachment; file_name=$filname"wink;

echo $filname;

exit;

That's the code and the pastebin link is:
Re: Php Script Giving Me Headache: Help! by face2tu: 12:25pm On Sep 30, 2011
Re: Php Script Giving Me Headache: Help! by Mobinga: 6:20pm On Sep 30, 2011
Are the two codes in 1 file? If so remove the html tags. LINE 1 to 6

<html>
<head>
<title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

And besides thats not the spelling of length, LINE 28

And again what are you doing with $result after querying the DB?
Re: Php Script Giving Me Headache: Help! by Nobody: 9:32pm On Sep 30, 2011
@mobinga whats up whats ur email addy?

(1) (Reply)

Does Any Functional Programmer Here Use Julia? / Random Problems To Measure Algorithmic Efficiency (asymptotic Analysis) / Php File Handling Help Needed

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.