Forum Moderators: coopster

Message Too Old, No Replies

Help with PHP file function

Problems with PHP file ()

         

Godster

7:06 pm on Nov 3, 2008 (gmt 0)

10+ Year Member



Hi guys,

First time to the forum so please be gentle.

Have just started out in PHP and applications development (home taught) and trying to implement a script that will help me to upload plant products to a database, withouth doing this line by line. I have been successful in using the form to input all the field variables with the exception of the image url for the thumb and main image.

At oresent im inteding to store all of this in the same products table but cannot get the file upload function to work. I get no errors at all, despite my attempts to add some error switching. The result however is no images in either the final or desitnation folder and no image urls added to the respective fields in my database.

I have modified the php.ini to tagert the usual suspects of file size, temp folder etc. but still cant seem to get anywhere with this function.

I have to admit the script is a mash-up of various resources in attempt to get it to do everything I want and probably (time permitting) should have been drafted from scratch.

Any help would be massively appreciated.

<?php

include 'dbconnect.php';

// $strRoot = $_SERVER['DOCUMENT_ROOT']; //Root path
$strThumbDir = "E:/WebDev/ProVeg/provegcom/images/thumbs"; // Path To Thumbnails Directory
$strMainDir = "E:/WebDev/ProVeg/provegcom/images"; // Path To Main Directory

var_dump($_POST);
echo $HTTP_POST_VARS['mode'];
echo $strMainDir;

if(isset($_POST["mode"])) {

if ($_POST["mode"] == "addtrial") {

$strSpecies = $_POST['strSpecies'];
$strVariety = $_POST['strVariety'];
$strBotanicalName = $_POST['strBotanicalName'];
$strFamily = $_POST['strFamily'];
$strClass = $_POST['strClass'];
$strSeedCount = $_POST['strSeedCount'];
$strConcentration = $_POST['strConcentration'];
$strDescription = $_POST['strDescription'];
$strGermination = $_POST['strGermination'];
$strNutrition = $_POST['strNutrition'];
$strPestResistance = $_POST['strPestResistance'];
$strOther = $_POST['strOther'];
$fCanFreeze = $_POST['fCanFreeze'];

//Set errors to false
$errorFlagged = false;

if ($_FILES['strThumbImage']['type'] == "image/jpg" ¦¦ $_FILES['strThumbImage']['type'] == "image/jpeg" ¦¦ $_FILES['strThumbImage']['type'] == "image/pjpeg") {

// Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
//$file_ext = strrchr($imagefile_name, '.');

$strTempThumbName = $_FILES['strThumbImage']['tmp_name'];
$strThumbName = $_FILES['strThumbImage']['name'];

$strNewLocationThumb = $strThumbDir . $strThumbName;

if (move_uploaded_file($strTempThumbName, $strNewLocationThumb)) {
echo "Thumbnail moved :: OK<br/>";
}

} //end if filetype

if ($_FILES['upload']['error'] > 0) {

switch ($_FILES['upload']['error']) {

case 1:
print 'The file is to big.'; // php installation max file size error
exit;
break;
case 2:
print 'The file is to big.'; // form max file size error
exit;
break;
case 3:
print 'Only part of the file was uploaded';
exit;
break;
case 4:
print 'No file was uploaded</p>';
exit;
break;
case 6:
print "Missing a temporary folder.";
exit;
break;
case 7:
print "Failed to write file to disk";
exit;
break;
case 8:
print "File upload stopped by extension";
exit;
break;
}//End of switch
}//End of error if.

if ($_FILES['strMainImage']['type'] == "image/jpg" ¦¦ $_FILES['strMainImage']['type'] == "image/jpeg" ¦¦ $_FILES['strMainImage']['type'] == "image/pjpeg") {

// Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
//$file_ext = strrchr($imagefile_name, '.');

$strTempMainName = $_FILES['strMainImage']['tmp_name'];
$strMainName = $_FILES['strMainImage']['name'];

$strNewLocationMain = $strMainDir . $strMainName;

if (move_uploaded_file($strTempMainName, $strNewLocationMain)) {
echo "Main image moved :: OK<br/>";
}

} //end if filetype

$sqlTrialAdd = "INSERT INTO pv_datasheets_09(strSpecies, strVariety, strBotanicalName, strFamily, strClass, strSeedCount, strConcentration, strDescription, strGermination, strNutrition, strPestResistance, strOther, fCanFreeze, strNewLocationThumb, strNewLocationMain)
VALUES('$strSpecies', '$strVariety', '$strBotanicalName', '$strFamily', '$strClass', '$strSeedCount', '$strConcentration', '$strDescription', '$strGermination', '$strNutrition', '$strPestResistance', '$strOther', '$fCanFreeze', '$strNewLocationThumb', '$strNewLocationMain')";

echo $sqlTrialAdd;

mysql_query($sqlTrialAdd) or die(mysql_error());

echo "<br />Done!";

}

}
?>

Many apologies if I have not posted the code in the correct fomat.

Godster

dreamcatcher

7:29 am on Nov 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Godster, welcome to Webmaster World. :)

Have you set your form to actually process uploads?

<form action="file.php" action="post" enctype="multipart/form-data">

dc

Godster

8:00 am on Nov 4, 2008 (gmt 0)

10+ Year Member



Hi dreamcatcher, thanks for the warm wlecome.

I have inserted the code for my form as below. I believe I have set the form action correctly but some advise would obviously be very useful as im not making any progress fast.


<form enctype="multipart/form-data" action="addtrial.php" method="POST">
<input name="mode" type="hidden" value="addtrial" />
Species: <input name="strSpecies" type="text" size="50" value="" />
<br />
<br />
Variety: <input name="strVariety" type="text" size="50" value="" />
<br />
<br />
Botanical Name: <input name="strBotanicalName" type="text" size="50" value="" />
<br />
<br />
Class: <input name="strClass" type="text" size="50" value="" />
<br />
<br />
Seed Count: <input name="strSeedCount" type="text" size="5" value="" />
<br />
<br />
Concentration: <input name="strConcentration" type="text" size="50" value="" />
<br />
<br />
Description: <br />
<textarea name="strDescription" textarea cols="50" rows="5"></textarea>
<br />
<br />
Germination: <br />
<textarea name="strGermination" textarea cols="50" rows="5"></textarea>
<br />
<br />
Nutrition: <br />
<textarea name="strNutrition" textarea cols="50" rows="5"></textarea>
<br />
<br />
Pest Resistance: <br />
<textarea name="strPestResistance" textarea cols="50" rows="5"></textarea>
<br />
<br />
Other: <br />
<textarea name="strOther" textarea cols="50" rows="5"></textarea>
<br />
<br />
Can freeze: <br />
<input type="radio" name="fCanFreeze" value="1" />Yes
<input type="radio" name="fCanFreeze" value="0" />No
<br />
<br/>
Thumb image:
<br />
<input name="strThumbImage" type="file" size="30" />
<br />
Main image:
<br/>
<input name="strMainImage" type="file" size="30" />
<br />
<br />
<input name="subForm" type="submit" value="Add trial" />
</form>

[edited by: Godster at 8:01 am (utc) on Nov. 4, 2008]

Godster

6:53 pm on Nov 4, 2008 (gmt 0)

10+ Year Member



Anyone help further with this issue please?

jatar_k

7:00 pm on Nov 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could try doing a couple things

try dumping the $_FILES array to see what's there

echo '<pre>';
print_r($_FILES);
echo '</pre>';

then you could also add an else here to handle the false

if (move_uploaded_file($strTempThumbName, $strNewLocationThumb)) {
echo "Thumbnail moved :: OK<br/>";
} else {
echo 'thumb could not be moved';
}

and the same on the main image move

are your errors set to show in the browser (parse errors and such)?

Godster

7:38 am on Nov 5, 2008 (gmt 0)

10+ Year Member



Thanks jatar i'll give that a go.

Can I clarify what you mean by "are your errors set to show in the browser (parse errors and such)?

"....I am relatively new to php a teaching myself in the process, how would i know and what weould I need to change to ensure they are set to display? I do get errors when running some script e.g. failed on line xx

jatar_k

5:29 pm on Nov 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I do get errors when running some script e.g. failed on line xx

that is what I meant. It is important to know what's being seen when someone says they're not seeing any errors. ;)

there is a setting in php.ini called 'display_errors' that controls it.

Godster

3:56 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



Ok thanks for confirming jatar. I think in that case it is safe to assume that i'm recieving errors, but am obviously not handling those in relation to the file function correctly, so as to ascertain where the problem is occuring.

I'm at a bit of a loss aprat from re-writing the whole script from scratch!

jatar_k

2:32 pm on Nov 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you could look at this upload script, it works
[webmasterworld.com...]

something else you could look at is permissions, though it looks like you're on windows, so it probably doesn't matter.

When I piece things together I often roll back step by step to see which step went horribly awry. I save versions of the code that work along the way, or just leave my editor open for infinite undos ;)