This topic is locked
[SOLVED]

 Add directory to Image path

8/18/2012 3:26:35 AM
PHPRunner General questions
L
luke author

Hello everyone,
I have a problem with my php application of trucks.
For each truck there is a form that describes technical details and shows at max 6 images.
What I should want is to set up automatic saving of every image that i upload to a path built from the default plus the name of plate of the truck.
For example:
I have an image name "truck.jpg" and a truck with plate "CN123BN". When i upload the image, normally it is put on default path (files/). In my case I would like it to be uploaded into the path "files/CN123BN/truck.jpg". This for each of 6 images that i can upload.
It is possible?
I would also like creating thumbnails of images

L
luke author 8/18/2012



Check this article: http://xlinesoft.com/phprunner/docs/upload_files_to_users_folders.htm


I have already read that article but in my case i have 6 images that mean 6 fields and not only one...

C
cgphp 8/18/2012

You have to adapt that code to your needs.

L
luke author 8/19/2012



You have to adapt that code to your needs.


How do it?

Sergey Kornilov admin 8/19/2012

Luke,
post your existing code from BeforeAdd event here. We also need to know which field stores the truck plate.

L
luke author 8/20/2012

Truck plate field: "Plate"

Truck images fields: "Image1", "Image2", "Image3", "Image4", "Image5", "Image6"
This is my code in Before/Add event:

global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["valueImage1".postvalue("id")]["tmp_name"])

{

//create directory if don't exist

$dirname= "files/".$values["Plate"];

if (is_dir($dirname)) {

} else {

mkdir("files/".$values["Plate"], 0777);

}

$pageObject->filesToMove[$key]->destFilename =

$values["Plate"]."/".$values["Image1"];

}

}

$values["Image1"] = $values["Plate"]."/".$values["Image1"];

Sergey Kornilov admin 8/20/2012

Your code looks good.
Could you clarify what's not working for you?

L
luke author 8/20/2012



Your code looks good.
Could you clarify what's not working for you?


The code works fine only if i had one image, but in my case i have 6 images: how can i pass 6 variables of the images?

Sergey Kornilov admin 8/20/2012

You need to create a similar code for each image field:

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_Image1_".postvalue("id")]["tmp_name"])

{

...

}

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_Image2_".postvalue("id")]["tmp_name"])

{

...

}


etc

L
luke author 8/22/2012



You need to create a similar code for each image field:

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_Image1_".postvalue("id")]["tmp_name"])

{

...

}

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_Image2_".postvalue("id")]["tmp_name"])

{

...

}


etc


Thanks a lot! I'll try it

L
luke author 8/24/2012



Thanks a lot! I'll try it


I try with this code:
global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["valueImage1".postvalue("id")]["tmp_name"])

{

//create directory if don't exist

$dirname= "files/".$values["Plate"];

if (is_dir($dirname)) {

} else {

mkdir("files/".$values["Plate"], 0777);

}

$pageObject->filesToMove[$key]->destFilename =

$values["Plate"]."/".$values["Image1"];

}

$values["Image1"] = $values["Plate"]."/".$values["Image1"];
if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["valueImage2".postvalue("id")]["tmp_name"])

{

//create directory if don't exist

$dirname= "files/".$values["Plate"];

if (is_dir($dirname)) {

} else {

mkdir("files/".$values["Plate"], 0777);

}

$pageObject->filesToMove[$key]->destFilename =

$values["Plate"]."/".$values["Image2"];

}

$values["Image2"] = $values["Plate"]."/".$values["Image2"];

}
I have 2 problems:

  • if i upload both image1 and image 2, the first is not copied to the folder, but the record is registered with the right link, the second is correct.
  • the images that i upload is not viewed as thubnails, but with is real size. For example: if i upload an image with resolution 2560×1600, my web page resize to that.