Applying value to select option in form.

The form I’m creating allows the admin to upload images to a directory. The form works as expected but now I’m trying to have the directory be different according to the option selected. The html output is as follows :

[php]

<?php foreach ( $results['pages'] as $page ) { ?> <?php echo $page->pagename?> <?php } ?>

Submit

<?php if ( isset( $results['status'] ) ) { ?>
  <p><?php echo $results['status'] ?></p>
<?php } ?> [/php]

The php script for the form is as follows :

[php]
$results = array();
$data = Page::getList();
$results[‘pages’] = $data[‘results’];
$results[‘totalRows’] = $data[‘totalRows’];
function getExtension($str) {
$tmp = explode(’.’, $str);
return $tmp[1];}
if (isset($_POST[‘submit’])) {
$pageImgFolder = $page->pagename;
$image = $_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
if ($image){
$filename = stripslashes($_FILES[‘file’][‘name’]);
$allowedExts = array(“image/jpeg”,“image/pjpeg”);
foreach ($allowedExts as $img_type) {
if ($img_type == $_FILES[“file”][“type”]) {
$typeOK = true;
if (file_exists(The_IMAGES_PATH . “/” . $pageImgFolder . $_FILES[“file”][“name”])) {
$results[‘status’] = "Text to display ";
}
else {
$image = $_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$filename = stripslashes($_FILES[‘file’][‘name’]);
$extension = getExtension($filename);
$extension = strtolower($extension);
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth = 290;
$newheight = 190;
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = The_IMAGES_PATH . “/” . $pageImgFolder . $_FILES[“file”][“name”];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
$results[‘imgLoaded’] = $_FILES[“file”][“name”];
$results[‘status’] = "Text to display ";}break;}
else { $typeOK = false;
$results[‘status’] = "Text to display ";
}
}
}
else {
$results[‘status’] = "Text to display ";
}
}
[/php]

The script works as is but will not upload to the directory of choice using the select option. The error that I’m receiving is - Undefined variable: page on the line below:

[php]
$pageImgFolder = $page->pagename;
[/php]

The select box list the names so that part works. My question is how to define the variable and once it is defined will the value be excepted as far as saving it to the directory of choice? Help on this would be awesome in my learning experience and so much appreciated.

Also note that “pagename” is “foldername” and it hasn’t been added to the create page form or mysql table I’m just running the test on this manually. by creating the folder manually. This form doesn’t deal with the database anyways, only to display the select options list which it does that with no problems. This post is only to save in the directory of listed choice.

you have to give the select option a name
[php]

<?php foreach ( $results['pages'] as $page ) { ?> <?php echo $page->pagename?> <?php } ?> [/php] then in the processing part use $_POST['foldername'] in the image path

let me know if that is what you are talking about :wink:

@plintu - I’ve tried this on the following lines of php :

[php]
$pageImgFolder = The_IMAGES_PATH . $_POST[‘foldername’];

  // file_exists()

if (file_exists($pageImgFolder . $_FILES[“file”][“name”])) {}

  // This is where I'm assuming the move_uploaded_file() is happening but I'm not actually sure.

$filename = $pageImgFolder . $_FILES[“file”][“name”];
imagejpeg($tmp,$filename,85);
imagedestroy($src);
imagedestroy($tmp);
[/php]

All this is doing is applying the name(value) on the file name when uploaded but it still goes to the default path [The_IMAGES_PATH]. At least I’m getting somewhere but where and when is the move uploaded file happening for sure cause I’m a bit confused on this actually. I’ll keep trying different things here until I receive a reply. Thanks a lot for this help, always appreciated.

It just worked. This is what I applied.

[php]
$pageImgFolder = The_IMAGES_PATH . $_POST[‘foldername’] . “/”;
[/php]

I’m jumping for joy here. Thanks a lot @plintu, this has been a great help.

Glad to help! marking topic solved :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service