tricky php, can anyone help
tricky php, can anyone help
am 16.01.2008 08:32:17 von matpac4
I'm a bit stuck! Please can someone help.
Here's what I'm trying to build:
1. Webform which puts variables into a database (each with unique ID
field)
2. Webform also allows attachment which uploads the file to specific
directory
3. On submission of webform for a file on the directory to be
duplicated and renamed "unique ID".php and placed in a specific folder
I have completed 1 + 2, but am stuck with the 3rd.
A few of my thoughts on this are:
Regarding the creation/duplication and renaming of the .php file,
maybe I can treat this as a file upload but from the server - a little
confusing, but I'm thinking along the lines of how the file upload
works above, except this would be a hidden field in the submission
form, and I'd have to set the path to the file on the server in the
hidden field also - not sure how to do this though...
....Plus this also leaves the problem of giving the duplicated file
it's related "uniqueid.php" name.
The contents of the php file that I'm wanting to create/duplicate will
all be the same code each time the form is submitted. I will have code
in these files which reads the file name and displays the proper stuff
related to the file name.
But I need a way of getting the unique id to the file name and am not
sure of a way to do this, especially with the problem of when several
people are completing the form at the same time. I need to make sure
that the number given to the duplicated .php file isn't associated
more than once.
If anyone can help me with any bitesize portion of this I'd appreciate
it.
Thanks for any help
Re: tricky php, can anyone help
am 16.01.2008 08:59:54 von Marlin Forbes
Hi there,
matpac4@gmail.com wrote:
> But I need a way of getting the unique id to the file name and am not
> sure of a way to do this, especially with the problem of when several
> people are completing the form at the same time. I need to make sure
> that the number given to the duplicated .php file isn't associated
> more than once.
When handling uploaded files in PHP, the $_FILES array holds a variable:
$_FILES['userfile']['tmp_name']
The tmp_name is guaranteed to be unique within the folder where the
temporary uploaded files are stored. You could strip the filename
portion of that out and use it in your destination folder.
For more details on file uploads and the temp folder handling, see the
entry under Features/Handling file uploads in the manual.
Or alternately, you can use the php function tempname, detailed under
Filesystem Functions in the manual.
Regards,
Marlin Forbes
Data Shaman
datashaman.com
Re: tricky php, can anyone help
am 16.01.2008 09:20:57 von matpac4
On Jan 16, 2:59 am, Marlin Forbes <"marlinf datashaman
wrote:
> Hi there,
>
> matp...@gmail.com wrote:
> > But I need a way of getting the unique id to the file name and am not
> > sure of a way to do this, especially with the problem of when several
> > people are completing the form at the same time. I need to make sure
> > that the number given to the duplicated .php file isn't associated
> > more than once.
>
> When handling uploaded files in PHP, the $_FILES array holds a variable:
>
> $_FILES['userfile']['tmp_name']
>
> The tmp_name is guaranteed to be unique within the folder where the
> temporary uploaded files are stored. You could strip the filename
> portion of that out and use it in your destination folder.
>
> For more details on file uploads and the temp folder handling, see the
> entry under Features/Handling file uploads in the manual.
>
> Or alternately, you can use the php function tempname, detailed under
> Filesystem Functions in the manual.
>
> Regards,
> Marlin Forbes
> Data Shaman
> datashaman.com
I'm not sure that this would solve the problems either, because I'd
like to try and control to some extent the unique ID's. In the same
way that when the form is submitted, the values enter the db and each
row is given a unique ID, I need the attachment (which I currently
have being uploaded with its name) to have its name changed to the
unique ID and the other action i needed was for a php file on the
server to be duplicated into a folder and be renamed "unique ID".php
The name of the image I'm uploading needs to have the same name as the
php file.
This process of uploading the attachment, duplicating/copying and
renaming the php file on server and entering the table contents into
the DB could happen thousands of times - I would ideally like the
names of the php pages to be 1001.php, 1002.php, 1003.php etc. (and
the associated attachment uploads to be 1001.gif, 1002.gif, 1003.gif
These dont necessarily need to be the same as the unique number thats
given to the mysql contents - but I'll have to do something to be able
to reference the db contents with the file names later (maybe add this
info to an extra table)....
do you have any ideas on how to make the names sequential like this
yet only allocate them once so that there's no problems with more than
one person filling in the form at any 1 time.
Do you have any experience with this code for duplicating a file on
the server, I cant get it to work:
$source_file = 'dir/folder/template.php';
$dest_file = 'dir/newfolder/newname.php';
copy($source_file, $dest_file);
You can see how I'm trying to copy this folder over and rename it.
Eventually I'd like to substitute 'newname.php' with '$uniqueID.php'
But if you can help me with the baby steps of getting any file to copy
and rename it'd really help.
Thx
Re: tricky php, can anyone help
am 16.01.2008 09:51:07 von Luuk
schreef in bericht
news:d3dba30e-9464-4818-af9a-9732133bf4c7@m34g2000hsb.google groups.com...
> On Jan 16, 2:59 am, Marlin Forbes <"marlinf datashaman
> wrote:
>> Hi there,
>>
>> matp...@gmail.com wrote:
>> > But I need a way of getting the unique id to the file name and am not
>> > sure of a way to do this, especially with the problem of when several
>> > people are completing the form at the same time. I need to make sure
>> > that the number given to the duplicated .php file isn't associated
>> > more than once.
>>
>> When handling uploaded files in PHP, the $_FILES array holds a variable:
>>
>> $_FILES['userfile']['tmp_name']
>>
>> The tmp_name is guaranteed to be unique within the folder where the
>> temporary uploaded files are stored. You could strip the filename
>> portion of that out and use it in your destination folder.
>>
>> For more details on file uploads and the temp folder handling, see the
>> entry under Features/Handling file uploads in the manual.
>>
>> Or alternately, you can use the php function tempname, detailed under
>> Filesystem Functions in the manual.
>>
>> Regards,
>> Marlin Forbes
>> Data Shaman
>> datashaman.com
>
> I'm not sure that this would solve the problems either, because I'd
> like to try and control to some extent the unique ID's. In the same
> way that when the form is submitted, the values enter the db and each
> row is given a unique ID, I need the attachment (which I currently
> have being uploaded with its name) to have its name changed to the
> unique ID and the other action i needed was for a php file on the
> server to be duplicated into a folder and be renamed "unique ID".php
>
> The name of the image I'm uploading needs to have the same name as the
> php file.
>
> This process of uploading the attachment, duplicating/copying and
> renaming the php file on server and entering the table contents into
> the DB could happen thousands of times - I would ideally like the
> names of the php pages to be 1001.php, 1002.php, 1003.php etc. (and
> the associated attachment uploads to be 1001.gif, 1002.gif, 1003.gif
>
> These dont necessarily need to be the same as the unique number thats
> given to the mysql contents - but I'll have to do something to be able
> to reference the db contents with the file names later (maybe add this
> info to an extra table)....
>
> do you have any ideas on how to make the names sequential like this
> yet only allocate them once so that there's no problems with more than
> one person filling in the form at any 1 time.
>
>
> Do you have any experience with this code for duplicating a file on
> the server, I cant get it to work:
>
> $source_file = 'dir/folder/template.php';
> $dest_file = 'dir/newfolder/newname.php';
>
> copy($source_file, $dest_file);
>
> You can see how I'm trying to copy this folder over and rename it.
> Eventually I'd like to substitute 'newname.php' with '$uniqueID.php'
>
> But if you can help me with the baby steps of getting any file to copy
> and rename it'd really help.
>
> Thx
try the example on http://nl3.php.net/manual/en/function.copy.php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
probably you get the result "failed to copy $file...\n"
because your webserver has nog enough rights...
Re: tricky php, can anyone help
am 16.01.2008 14:26:44 von colin.mckinnon
On 16 Jan, 08:51, "Luuk" wrote:
> schreef in berichtnews:d3dba30e-9464-4818-af9a-9732133bf4c7@m34g2000hsb .googlegroups.com...
>
>
>
> > On Jan 16, 2:59 am, Marlin Forbes <"marlinf datashaman
> > wrote:
> >> Hi there,
>
> >> matp...@gmail.com wrote:
> >> > But I need a way of getting the unique id to the file name and am not
> >> > sure of a way to do this, especially with the problem of when several
> >> > people are completing the form at the same time. I need to make sure
> >> > that the number given to the duplicated .php file isn't associated
> >> > more than once.
>
> >> When handling uploaded files in PHP, the $_FILES array holds a variable:
>
> >> $_FILES['userfile']['tmp_name']
>
> >> The tmp_name is guaranteed to be unique within the folder where the
> >> temporary uploaded files are stored. You could strip the filename
> >> portion of that out and use it in your destination folder.
>
Certainly you shouldn't preallocate names and store them in the form -
that's just asking for trouble. If you're using MySQL and have an
autoincrement id column, then you can find the value used by the last
insert with mysql_insert_id().
> >> For more details on file uploads and the temp folder handling, see the
> >> entry under Features/Handling file uploads in the manual.
....
> > This process of uploading the attachment, duplicating/copying and
> > renaming the php file on server and entering the table contents into
> > the DB could happen thousands of times - I would ideally like the
> > names of the php pages to be 1001.php, 1002.php, 1003.php etc. (and
> > the associated attachment uploads to be 1001.gif, 1002.gif, 1003.gif
>
If you've got a large number of files, you should definitely think
about splitting this into directories:
function get_dir($filenumber, $level)
{
$out='';
for ($x=0; $x<$level; $x++) {
$out.=substr($filenumber,$x,1) . '/';
}
$out.=substr($filenumber,$level);
return($out);
}
.....
get_dir(10203, 3) = '1/0/2/03'
get_dir('94522378.gif', 4) = '9/4/5/2/2378.gif'
> > You can see how I'm trying to copy this folder over and rename it.
> > Eventually I'd like to substitute 'newname.php' with '$uniqueID.php'
>
!!!
You're going to let people upload php code to your server! Be very
careful!! If its inside the doc root and you don't reconfigure your
webserver it means the code wil be executed!!!
> try the example onhttp://nl3.php.net/manual/en/function.copy.php
>
> $file = 'example.txt';
> $newfile = 'example.txt.bak';
>
> if (!copy($file, $newfile)) {
> echo "failed to copy $file...\n";}
>
> ?>
no - use move_uploaded_file() instead - RTFM
C.
Re: tricky php, can anyone help
am 16.01.2008 14:57:07 von Luuk
"C. (http://symcbean.blogspot.com/)" schreef in
bericht
news:95db974c-57ba-419e-9ada-d9f4dcc98c92@u10g2000prn.google groups.com...
>> try the example onhttp://nl3.php.net/manual/en/function.copy.php
>>
>> $file = 'example.txt';
>> $newfile = 'example.txt.bak';
>>
>> if (!copy($file, $newfile)) {
>> echo "failed to copy $file...\n";}
>>
>> ?>
>
> no - use move_uploaded_file() instead - RTFM
>
> C.
it was the manual i quoted......
OP was using copy() and was wondering why it did not work,
not questioning himself if there was another (or better) option.
;-)
Re: tricky php, can anyone help
am 16.01.2008 16:16:02 von matpac4
On Jan 16, 8:26 am, "C. (http://symcbean.blogspot.com/)"
wrote:
> On 16 Jan, 08:51, "Luuk" wrote:
>
>
>
> > schreef in berichtnews:d3dba30e-9464-4818-af9a-9732133bf4c7@m34g2000hsb .googlegroups.com...
>
> > > On Jan 16, 2:59 am, Marlin Forbes <"marlinf datashaman
> > > wrote:
> > >> Hi there,
>
> > >> matp...@gmail.com wrote:
> > >> > But I need a way of getting the unique id to the file name and am not
> > >> > sure of a way to do this, especially with the problem of when several
> > >> > people are completing the form at the same time. I need to make sure
> > >> > that the number given to the duplicated .php file isn't associated
> > >> > more than once.
>
> > >> When handling uploaded files in PHP, the $_FILES array holds a variable:
>
> > >> $_FILES['userfile']['tmp_name']
>
> > >> The tmp_name is guaranteed to be unique within the folder where the
> > >> temporary uploaded files are stored. You could strip the filename
> > >> portion of that out and use it in your destination folder.
>
> Certainly you shouldn't preallocate names and store them in the form -
> that's just asking for trouble. If you're using MySQL and have an
> autoincrement id column, then you can find the value used by the last
> insert with mysql_insert_id().
>
> > >> For more details on file uploads and the temp folder handling, see the
> > >> entry under Features/Handling file uploads in the manual.
> ...
> > > This process of uploading the attachment, duplicating/copying and
> > > renaming the php file on server and entering the table contents into
> > > the DB could happen thousands of times - I would ideally like the
> > > names of the php pages to be 1001.php, 1002.php, 1003.php etc. (and
> > > the associated attachment uploads to be 1001.gif, 1002.gif, 1003.gif
>
> If you've got a large number of files, you should definitely think
> about splitting this into directories:
>
> function get_dir($filenumber, $level)
> {
> $out='';
> for ($x=0; $x<$level; $x++) {
> $out.=substr($filenumber,$x,1) . '/';
> }
> $out.=substr($filenumber,$level);
> return($out);
>
> }
>
> ....
> get_dir(10203, 3) = '1/0/2/03'
>
> get_dir('94522378.gif', 4) = '9/4/5/2/2378.gif'
>
> > > You can see how I'm trying to copy this folder over and rename it.
> > > Eventually I'd like to substitute 'newname.php' with '$uniqueID.php'
>
> !!!
>
> You're going to let people upload php code to your server! Be very
> careful!! If its inside the doc root and you don't reconfigure your
> webserver it means the code wil be executed!!!
>
> > try the example onhttp://nl3.php.net/manual/en/function.copy.php
> >
> > $file = 'example.txt';
> > $newfile = 'example.txt.bak';
>
> > if (!copy($file, $newfile)) {
> > echo "failed to copy $file...\n";}
>
> > ?>
>
> no - use move_uploaded_file() instead - RTFM
>
> C.
Thanks Marlin, Luuk and C. for your replies.
I'm trying to work with them but I am a beginner and this is a tricky
problem.
C. - a couple of things, I'd like to keep it simple with a single
directory for the moment because other things (code) rely on this fact
at the moment.
Also I'm only allowing users to upload images. I just need a certain
php file to be created everytime the form is submit. And I need it to
have a sequentially increasing number for it's name taht is somehow
kept on a table or sourced from a table so that I can reference this
number to the submitters name in future.
I'll look through the code again, just not sure if copy is best if I
already have the file I want creating on the server, and just to copy
it and rename it on form submit.
.... I've just been thinking and maybe I have to make this form a 2
step process, maybe ask them half their details, then click next (this
will create a row on another tbl containg 'persons name', 'unique
incremental file number' (issued on input to db) and there telephone
or something...
....the second part of the form will look down the table for the
'unique incremental filenumber' where persons name = bob, and
telephone = 2123456789 (and it will already have these details (bob
and tel) from the first half of the form) - then it will have a unique
id for that form submission and the number retrieved can then be used
to rename the copied file on the server and rename the attached
image...
Hassle that it's a 2 step form but might work - what do you think?
If there's a way for this to work then I just need to figure out how
to copy a file already on the server to a new location and rename
(I'll check your suggestion Luuk)
Thanks
Re: tricky php, can anyone help
am 16.01.2008 16:58:43 von Luuk
schreef in bericht
news:168d2f13-a113-4b53-8f54-9e7e7a21fc11@e23g2000prf.google groups.com...
> On Jan 16, 8:26 am, "C. (http://symcbean.blogspot.com/)"
>>
>> no - use move_uploaded_file() instead - RTFM
>>
>> C.
>
>
> If there's a way for this to work then I just need to figure out how
> to copy a file already on the server to a new location and rename
> (I'll check your suggestion Luuk)
>
> Thanks
copy and rename can be done in 1 step, as C suggested
http://nl2.php.net/move_uploaded_file
Re: tricky php, can anyone help
am 17.01.2008 00:16:53 von matpac4
Thanks Guys I have it all working perfectly it seems!
Thanks again for your help very useful.
M