Jump to content

[SOLVED] File not Uploading and data not stored in DB


MP145

Recommended Posts

The images are not moved to the folder neither does the image name gets stored in the database. The rest of the data is stored successfully. Something is wrong and i can't figure out. please have a look at the code. Using PHP 5 and MySql 5

 

<?php
$conn = mysql_connect('localhost','xxx','yyyy') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('news',$conn) or trigger_error("SQL", E_USER_ERROR);

$title = $_POST['title'];
$snews = $_POST['snews'];
$lnews = $_POST['lnews'];
$thumb = $_FILES['thumb']['name'];
$tmpThumb = $_FILES['thumb']['tmp_name'];
$imageName1 = $_FILES['image1']['name'];
$tmpImageName1 = $_FILES['image1']['tmp_name'];
$txt1 = $_POST['txt1'];
$imageName2 = $_FILES['image2']['name'];
$tmpImageName2 = $_FILES['image2']['tmp_name'];
$txt2 = $_POST['txt2'];
$imageName3 = $_FILES['image3']['name'];
$tmpImageName3 = $_FILES['image3']['tmp_name'];
$txt3 = $_POST['txt3'];
$imageName4 = $_FILES['image4']['name'];
$tmpImageName4 = $_FILES['image4']['tmp_name'];
$txt4 = $_POST['txt4'];
$imageName5 = $_FILES['image5']['name'];
$tmpImageName5 = $_FILES['image5']['tmp_name'];
$txt5 = $_POST['txt5'];
$addby = $_POST['addby'];
$nsource = $_POST['nsource'];
$surl = $_POST['surl'];

if ( file_exists($thumb) ){
    $filename = stripslashes($thumb);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name0 = time().'_news.'.$extension;
    $newname0 = "../images/news/".$image_name0;
    move_uploaded_file($tmpThumb, $newname0);
}
if ( file_exists($imageName1) ){
    $filename = stripslashes($imageName1);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name1 = time().'_news.'.$extension;
    $newname1 = "../images/news/".$image_name1;
    move_uploaded_file($tmpimageName1, $newname1);
}
if ( file_exists($imageName2) ){
    $filename = stripslashes($imageName2);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name2 = time().'_news.'.$extension;
    $newname2 = "../images/news/".$image_name2;
    move_uploaded_file($tmpimageName2, $newname2);
}
if ( file_exists($imageName3) ){
    $filename = stripslashes($imageName3);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name3 = time().'_news.'.$extension;
    $newname3 = "../images/news/".$image_name3;
    move_uploaded_file($tmpimageName3, $newname3);
}
if ( file_exists($imageName4) ){
    $filename = stripslashes($imageName4);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name4 = time().'_news.'.$extension;
    $newname4 = "../images/news/".$image_name4;
    move_uploaded_file($tmpimageName4, $newname4);
}
if ( file_exists($imageName5) ){
    $filename = stripslashes($imageName5);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name5 = time().'_news.'.$extension;
    $newname5 = "../images/news/".$image_name5;
    move_uploaded_file($tmpimageName5, $newname5);
}



$sql = "INSERT INTO newsitem (title, snews, lnews, thumb, image1, txt1, image2, txt2, image3, txt3, image4, txt4, image5, txt5, addby, nsource, surl, date) VALUES ('$title', '$snews', '$lnews', '$image_name0','$image_name1', '$txt1', '$image_name2', '$txt2', '$image_name3', '$txt3', '$image_name4', '$txt4', '$image_name5', '$txt5', '$addby', '$nsource', '$surl', now())";
mysql_query($sql) or die ("Error in query: $sql. ".mysql_error());

echo "<h4>NEWS SUCCESSFULLY ADDED</h4><br />";

}
?>

 

I know the code is too long but i dont know how to write foreach even after reading the sample at php.net and also how to input the name to the database by using foreach.

 

Thanks

Link to comment
Share on other sites

In order for uploading to work, your form must be correct, uploads must be enabled and correctly configured on your server, and the file size must be less than a few different size settings.

 

For problem #1, post your form. For problem #2, use a phpinfo(); statement and check what the file_uploads setting is.

 

For problem #3, all code must check if there are any errors before blindly attempting to use any kind of data. Since the size of a file or even if they selected a file to upload is out of your control, you must check for all possible errors. One of the size errors causes the $_FILES array to be empty (assuming you have eliminated problem #1 and #2.) The rest of the possible upload errors are detectable in the ['error'] element of the $_FILES variable.

 

To test for an empty $_FILES array -

if(empty($_FILES)){
    echo "The total size of the uploaded files exceeds the post_max_size setting";
    exit;  // there is no point in processing any further
}

 

Each different file you upload could have an error and needs to be tested. Example for your 'thumb' file  -

 

if($_FILES['thumb']['error'] != 0){
    echo "An upload error {$_FILES['thumb']['error']} occurred for the 'thumb' file";
} else {
    // this file uploaded correctly, put the form processing code for it here
}

 

Reference for all the above information: http://us2.php.net/features.file-upload

Link to comment
Share on other sites

ok, here goes :-

 

problem #1

My form page

<?php
session_start();

if($_SESSION['MemberLevel'] == ""){
  header("Location: index.html");
  }

elseif($_SESSION['MemberLevel'] == "1"){
}
?>
<html>

<head>
  <title>Add News Panel</title>
</head>

<body>
<center><font face="Verdana" size="2" color="#000000"><b>ADD NEWS</b></font></center>
<br><br><br />
<form action="processnews1.php" method="post" enctype="multipart/form-data">
<div align="center">
<table border="0" id="table1">
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>News Title :</b></font></td>
	<td align="left"><input type="text" name="title" size="74"></td>
</tr>
<tr>
	<td width="104" align="left"><font face="Verdana" size="2"><b>Short News :</b></font></td>
	<td align="left"><textarea rows="4" name="snews" cols="56"></textarea></td>
</tr>
<tr>
	<td width="104" align="left"><font face="Verdana" size="2"><b>Long News :</b></font></td>
	<td align="left"><textarea rows="13" name="lnews" cols="56"></textarea></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Thumbnail :</b></font></td>
	<td height="30" align="left"><input name="thumb" type="file" /></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image :</b></font></td>
	<td height="30" align="left"><input name="image1" type="file" /></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image Text :</b></font></td>
	<td height="30" align="left"><input type="text" name="txt1" size="74"></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image :</b></font></td>
	<td height="30" align="left"><input name="image2" type="file" /></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image Text :</b></font></td>
	<td height="30" align="left"><input type="text" name="txt2" size="74"></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image :</b></font></td>
	<td height="30" align="left"><input name="image3" type="file" /></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image Text :</b></font></td>
	<td height="30" align="left"><input type="text" name="txt3" size="74"></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image :</b></font></td>
	<td height="30" align="left"><input name="image4" type="file" /></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image Text :</b></font></td>
	<td height="30" align="left"><input type="text" name="txt4" size="74"></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image :</b></font></td>
	<td height="30" align="left"><input name="image5" type="file" /></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Image Text :</b></font></td>
	<td height="30" align="left"><input type="text" name="txt5" size="74"></td>
</tr>
<tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Add By :</b></font></td>
	<td height="30" align="left"><input type="text" name="addby" size="74"></td>
</tr>
    <tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>News Source :</b></font></td>
	<td height="30" align="left"><input type="text" name="nsource" size="74"></td>
</tr>
    <tr>
	<td width="104" height="30" align="left"><font face="Verdana" size="2"><b>Source URL :</b></font></td>
	<td height="30" align="left"><input type="text" name="surl" size="74"></td>
</tr>
<tr>
	<td width="104" height="30" align="left"> </td>
	<td height="30" align="left"><input type="submit" value="Add News" /></td>
</tr>
</table>
</div>
</form>

</body>

</html>

 

Problem #2

 

I checked with phpinfo() and both the Local Value and Master Value is ON

 

Problem #3

(1)

When i use this, i get no errors and it displays the file name

if ( ($thumb != "") ){

     $image_name0 = stripslashes($thumb);

    echo "success $image_name0";
}

 

(2)

But, i don't get any error, just a blank page when i add this

if ( ($thumb != "") ){

     $image_name0 = stripslashes($thumb);
     $extension = getExtension($filename);


    echo "success $extension";

}

 

i have the error_reporting(E_ALL); at the top of the page after <?php tags

Link to comment
Share on other sites

OK, got it fixed..permanently i hope. I did not add the getExtension function, now it works.

 

function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

   if ( ($_FILES['thumb']['name'] != "") ){


     $extension = getExtension($_FILES['thumb']['name']);
     $image_name0 = $extension;


    echo "success $image_name0";

}

 

Thanks PFMaBiSmAd  ;D

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.