What am I missing here? - PHP

This is a discussion on What am I missing here? - PHP ; Good day all, I have been pulling my hair out trying to figure out what I'm doing wrong with this code. I am trying to create one AND ONLY ONE directory randomly named function. This code works perfectly the first ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

What am I missing here?

  1. Default What am I missing here?

    Good day all,

    I have been pulling my hair out trying to figure out what I'm doing
    wrong with this code. I am trying to create one AND ONLY ONE directory
    randomly named function. This code works perfectly the first time
    through and so long as the page is not refreshed everything is fine.
    Random directory name generated, ran against database to assure no dups,
    crates a dir based on that name, moves into that directory and puts
    the files there, all is well. But, when refresh is hit, this code
    continues to generate randomly generated directories and, using
    move_uploaded_file(), moves the files from tmp to new dir. Obviously,
    not good! The conditional shown here is on is_null(), it is not working,
    I have tried !isset($adIndex) at beginning of script the unset($adIndex)
    at end, this didn't work either.....

    The code:
    Random_Index($n) <--- Returns string of randomly created alphanumeric
    chars of $n length
    indexExist($index) <--- Returns true if $index already exists in database
    [snip] .......

    if(is_null($adIndex)) {
    $adIndex = Random_Index(8);

    // Check database for dups on index just created
    while($data->indexExist($adIndex)) {
    $adIndex = Random_Index(8);
    }
    }

    chdir('ad_images');
    mkdir($adIndex);
    chdir($adIndex);

    for($i = 1; $i <= count($_FILES); $i++) {

    if(($_FILES["file_$i"]["type"] == "image/gif")
    || ($_FILES["file_$i"]["type"] == "image/jpeg")
    || ($_FILES["file_$i"]["type"] == "image/pjpeg")
    || ($_FILES["file_$i"]["type"] == "image/png")
    && ($_FILES["file_$i"]["size"] < 5000000)
    && ($_FILES["file_$i"]["name"] !== '')) {

    if ($_FILES["file_$i"]["error"] > 0) {
    echo "Return code: " . $_FILES["$i"]["error"] . "<br />";
    } else {
    if(file_exists($_FILES["file_$i"]["name"])) {
    echo $_FILES["file_$i"]["name"] . " already exists. <br />";
    } else {
    move_uploaded_file($_FILES["file_$i"]["tmp_name"],
    $_FILES["file_$i"]["name"]);
    if(file_exists($_FILES["file_$i"]["name"])) {
    echo "Image #$i" . $_FILES["file_$i"]["name"] . " <img
    src=" . $_FILES["file_$i"]["name"] . "\"<br />";
    } else {
    echo "Image " . $_FILES["file_$i"]["name"] . " did not
    upload.<br />";
    } // END if file_exists......
    } // END if file_exists.....
    } // END if $_FILES[error]
    } // END if($_FILES.....)
    } // END for() loop

    [/snip]

    Thanks for any help at all.


    ------------------------------------
    Gene Kelley
    Senior Open Source Software Engineer
    Advanced Design Solutions Team
    Network Solutions (MonsterCommerce)
    Swansea, Illinois, USA

  2. Default Re: What am I missing here?

    Jerry Stuckle wrote:
    > Gene Kelley wrote:
    >> Good day all,
    >>
    >> I have been pulling my hair out trying to figure out what I'm doing
    >> wrong with this code. I am trying to create one AND ONLY ONE
    >> directory randomly named function. This code works perfectly the
    >> first time through and so long as the page is not refreshed everything
    >> is fine. Random directory name generated, ran against database to
    >> assure no dups, crates a dir based on that name, moves into that
    >> directory and puts the files there, all is well. But, when refresh is
    >> hit, this code continues to generate randomly generated directories
    >> and, using move_uploaded_file(), moves the files from tmp to new dir.
    >> Obviously, not good! The conditional shown here is on is_null(), it is
    >> not working, I have tried !isset($adIndex) at beginning of script the
    >> unset($adIndex) at end, this didn't work either.....
    >>
    >> The code:
    >> Random_Index($n) <--- Returns string of randomly created alphanumeric
    >> chars of $n length
    >> indexExist($index) <--- Returns true if $index already exists in database
    >> [snip] .......
    >>
    >> if(is_null($adIndex)) {
    >> $adIndex = Random_Index(8);
    >>
    >> // Check database for dups on index just created
    >> while($data->indexExist($adIndex)) {
    >> $adIndex = Random_Index(8);
    >> }
    >> }
    >>
    >> chdir('ad_images');
    >> mkdir($adIndex);
    >> chdir($adIndex);
    >>
    >> for($i = 1; $i <= count($_FILES); $i++) {
    >> if(($_FILES["file_$i"]["type"] == "image/gif")
    >> || ($_FILES["file_$i"]["type"] == "image/jpeg")
    >> || ($_FILES["file_$i"]["type"] == "image/pjpeg")
    >> || ($_FILES["file_$i"]["type"] == "image/png")
    >> && ($_FILES["file_$i"]["size"] < 5000000)
    >> && ($_FILES["file_$i"]["name"] !== '')) {
    >> if ($_FILES["file_$i"]["error"] > 0) {
    >> echo "Return code: " . $_FILES["$i"]["error"] . "<br />";
    >> } else {
    >> if(file_exists($_FILES["file_$i"]["name"])) {
    >> echo $_FILES["file_$i"]["name"] . " already exists. <br />";
    >> } else {
    >> move_uploaded_file($_FILES["file_$i"]["tmp_name"],
    >> $_FILES["file_$i"]["name"]);
    >> if(file_exists($_FILES["file_$i"]["name"])) {
    >> echo "Image #$i" . $_FILES["file_$i"]["name"] . " <img
    >> src=" . $_FILES["file_$i"]["name"] . "\"<br />";
    >> } else {
    >> echo "Image " . $_FILES["file_$i"]["name"] . " did not
    >> upload.<br />";
    >> } // END if file_exists......
    >> } // END if file_exists.....
    >> } // END if $_FILES[error]
    >> } // END if($_FILES.....)
    >> } // END for() loop
    >>
    >> [/snip]
    >>
    >> Thanks for any help at all.
    >>
    >>
    >> ------------------------------------
    >> Gene Kelley
    >> Senior Open Source Software Engineer
    >> Advanced Design Solutions Team
    >> Network Solutions (MonsterCommerce)
    >> Swansea, Illinois, USA
    >>

    >
    > It's doing exactly what you told it to do - refresh sends the data to
    > the server, and your script is processing it.
    >
    > After moving the uploaded file, use header('Location...") to redirect to
    > a new page.
    >

    Thanks Jerry. I totally agree with you. Could require a design change
    then I guess. Now that you've brought that to the table (I was thinking
    the problem was just in the snippet of code I supplied), I should have
    explained that this snippet of code is within a form which is submitting
    to itself that is gathering other data which is then cleansed and
    validated and then, if any errors, re-populates the elements and
    provides suggested corrective actions.

    Although your suggestion is a good one and likely should have been
    designed that way initially, it really gets me to thinking (as
    programming often does) ... Is there really just no way to determine
    that a variable is already set and is holding a value, so don't create
    another one.... ???

    Thanks again for the good advice and bringing in a fresh new angle...

    --
    ------------------------------------
    Gene Kelley
    Senior Open Source Software Engineer
    Advanced Design Solutions Team
    Network Solutions (MonsterCommerce)
    Swansea, Illinois, USA

  3. Default Re: What am I missing here?

    Gene Kelley wrote:
    > Good day all,
    >
    > I have been pulling my hair out trying to figure out what I'm doing
    > wrong with this code. I am trying to create one AND ONLY ONE directory
    > randomly named function. This code works perfectly the first time
    > through and so long as the page is not refreshed everything is fine.
    > Random directory name generated, ran against database to assure no dups,
    > crates a dir based on that name, moves into that directory and puts the
    > files there, all is well. But, when refresh is hit, this code continues
    > to generate randomly generated directories and, using
    > move_uploaded_file(), moves the files from tmp to new dir. Obviously,
    > not good! The conditional shown here is on is_null(), it is not working,
    > I have tried !isset($adIndex) at beginning of script the unset($adIndex)
    > at end, this didn't work either.....
    >
    > The code:
    > Random_Index($n) <--- Returns string of randomly created alphanumeric
    > chars of $n length
    > indexExist($index) <--- Returns true if $index already exists in database
    > [snip] .......
    >
    > if(is_null($adIndex)) {
    > $adIndex = Random_Index(8);
    >
    > // Check database for dups on index just created
    > while($data->indexExist($adIndex)) {
    > $adIndex = Random_Index(8);
    > }
    > }
    >
    > chdir('ad_images');
    > mkdir($adIndex);
    > chdir($adIndex);
    >
    > for($i = 1; $i <= count($_FILES); $i++) {
    >
    > if(($_FILES["file_$i"]["type"] == "image/gif")
    > || ($_FILES["file_$i"]["type"] == "image/jpeg")
    > || ($_FILES["file_$i"]["type"] == "image/pjpeg")
    > || ($_FILES["file_$i"]["type"] == "image/png")
    > && ($_FILES["file_$i"]["size"] < 5000000)
    > && ($_FILES["file_$i"]["name"] !== '')) {
    >
    > if ($_FILES["file_$i"]["error"] > 0) {
    > echo "Return code: " . $_FILES["$i"]["error"] . "<br />";
    > } else {
    > if(file_exists($_FILES["file_$i"]["name"])) {
    > echo $_FILES["file_$i"]["name"] . " already exists. <br />";
    > } else {
    > move_uploaded_file($_FILES["file_$i"]["tmp_name"],
    > $_FILES["file_$i"]["name"]);
    > if(file_exists($_FILES["file_$i"]["name"])) {
    > echo "Image #$i" . $_FILES["file_$i"]["name"] . " <img
    > src=" . $_FILES["file_$i"]["name"] . "\"<br />";
    > } else {
    > echo "Image " . $_FILES["file_$i"]["name"] . " did not
    > upload.<br />";
    > } // END if file_exists......
    > } // END if file_exists.....
    > } // END if $_FILES[error]
    > } // END if($_FILES.....)
    > } // END for() loop
    >
    > [/snip]
    >
    > Thanks for any help at all.
    >
    >
    > ------------------------------------
    > Gene Kelley
    > Senior Open Source Software Engineer
    > Advanced Design Solutions Team
    > Network Solutions (MonsterCommerce)
    > Swansea, Illinois, USA
    >


    It's doing exactly what you told it to do - refresh sends the data to
    the server, and your script is processing it.

    After moving the uploaded file, use header('Location...") to redirect to
    a new page.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================


  4. Default Re: What am I missing here?

    Gene Kelley <gene@bizflowdesigns.com> wrote:

    >Jerry Stuckle wrote:
    >>
    >> It's doing exactly what you told it to do - refresh sends the data to
    >> the server, and your script is processing it.
    >>
    >> After moving the uploaded file, use header('Location...") to redirect to
    >> a new page.
    >>

    >Thanks Jerry. I totally agree with you. Could require a design change
    >then I guess. Now that you've brought that to the table (I was thinking
    >the problem was just in the snippet of code I supplied), I should have
    >explained that this snippet of code is within a form which is submitting
    >to itself that is gathering other data which is then cleansed and
    >validated and then, if any errors, re-populates the elements and
    >provides suggested corrective actions.
    >
    >Although your suggestion is a good one and likely should have been
    >designed that way initially, it really gets me to thinking (as
    >programming often does) ... Is there really just no way to determine
    >that a variable is already set and is holding a value, so don't create
    >another one.... ???


    To quote Jerry Stuckle from an earlier thread today: "Each time a script
    starts, it is a new program. It has no information from any previous
    script. The only way you can maintain values is through cookies or
    sessions."
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

  5. Default Re: What am I missing here?

    On Mar 13, 1:45 am, Tim Roberts <t...@probo.com> wrote:
    > Gene Kelley <g...@bizflowdesigns.com> wrote:
    > >Jerry Stuckle wrote:

    >
    > >> It's doing exactly what you told it to do - refresh sends the data to
    > >> the server, and your script is processing it.

    >
    > >> After moving the uploaded file, use header('Location...") to redirect to
    > >> a new page.

    >
    > >Thanks Jerry. I totally agree with you. Could require a design change
    > >then I guess. Now that you've brought that to the table (I was thinking
    > >the problem was just in the snippet of code I supplied), I should have
    > >explained that this snippet of code is within a form which is submitting
    > >to itself that is gathering other data which is then cleansed and
    > >validated and then, if any errors, re-populates the elements and
    > >provides suggested corrective actions.

    >
    > >Although your suggestion is a good one and likely should have been
    > >designed that way initially, it really gets me to thinking (as
    > >programming often does) ... Is there really just no way to determine
    > >that a variable is already set and is holding a value, so don't create
    > >another one.... ???

    >
    > To quote Jerry Stuckle from an earlier thread today: "Each time a script
    > starts, it is a new program. It has no information from any previous
    > script. The only way you can maintain values is through cookies or
    > sessions."
    > --
    > Tim Roberts, t...@probo.com
    > Providenza & Boekelheide, Inc.


    Maybe I as well am misunderstanding something in this post.

    So if a variable is NOT set. Then a function is called within the
    script based on this false positive in that, say $x, if it's TRUE that
    this variable ISN'T set then call a function that sets it, operates on
    it, then returns whatever based on this operation (i.e. if(!
    isset($x) ? functionThatDoesSetX() : moveAlongNotSettingX()

    At this point, if the script is reloaded, or even submitted in this
    case as the form is submitting unto itself, isn't it true that now
    this $x is set and functionThatDoesSetX() would NOT run?

    Interesting .... I will go learn and play in my sandbox for awhile.


  6. Default Re: What am I missing here?

    Gene Kelley wrote:
    > Jerry Stuckle wrote:
    >> Gene Kelley wrote:
    >>> Good day all,
    >>>
    >>> I have been pulling my hair out trying to figure out what I'm doing
    >>> wrong with this code. I am trying to create one AND ONLY ONE
    >>> directory randomly named function. This code works perfectly the
    >>> first time through and so long as the page is not refreshed
    >>> everything is fine. Random directory name generated, ran against
    >>> database to assure no dups, crates a dir based on that name, moves
    >>> into that directory and puts the files there, all is well. But, when
    >>> refresh is hit, this code continues to generate randomly generated
    >>> directories and, using move_uploaded_file(), moves the files from tmp
    >>> to new dir. Obviously, not good! The conditional shown here is on
    >>> is_null(), it is not working, I have tried !isset($adIndex) at
    >>> beginning of script the unset($adIndex) at end, this didn't work
    >>> either.....
    >>>
    >>> The code:
    >>> Random_Index($n) <--- Returns string of randomly created alphanumeric
    >>> chars of $n length
    >>> indexExist($index) <--- Returns true if $index already exists in
    >>> database
    >>> [snip] .......
    >>>
    >>> if(is_null($adIndex)) {
    >>> $adIndex = Random_Index(8);
    >>>
    >>> // Check database for dups on index just created
    >>> while($data->indexExist($adIndex)) {
    >>> $adIndex = Random_Index(8);
    >>> }
    >>> }
    >>>
    >>> chdir('ad_images');
    >>> mkdir($adIndex);
    >>> chdir($adIndex);
    >>>
    >>> for($i = 1; $i <= count($_FILES); $i++) {
    >>> if(($_FILES["file_$i"]["type"] == "image/gif")
    >>> || ($_FILES["file_$i"]["type"] == "image/jpeg")
    >>> || ($_FILES["file_$i"]["type"] == "image/pjpeg")
    >>> || ($_FILES["file_$i"]["type"] == "image/png")
    >>> && ($_FILES["file_$i"]["size"] < 5000000)
    >>> && ($_FILES["file_$i"]["name"] !== '')) {
    >>> if ($_FILES["file_$i"]["error"] > 0) {
    >>> echo "Return code: " . $_FILES["$i"]["error"] . "<br />";
    >>> } else {
    >>> if(file_exists($_FILES["file_$i"]["name"])) {
    >>> echo $_FILES["file_$i"]["name"] . " already exists. <br />";
    >>> } else {
    >>> move_uploaded_file($_FILES["file_$i"]["tmp_name"],
    >>> $_FILES["file_$i"]["name"]);
    >>> if(file_exists($_FILES["file_$i"]["name"])) {
    >>> echo "Image #$i" . $_FILES["file_$i"]["name"] . " <img
    >>> src=" . $_FILES["file_$i"]["name"] . "\"<br />";
    >>> } else {
    >>> echo "Image " . $_FILES["file_$i"]["name"] . " did not
    >>> upload.<br />";
    >>> } // END if file_exists......
    >>> } // END if file_exists.....
    >>> } // END if $_FILES[error]
    >>> } // END if($_FILES.....)
    >>> } // END for() loop
    >>>
    >>> [/snip]
    >>>
    >>> Thanks for any help at all.
    >>>
    >>>
    >>> ------------------------------------
    >>> Gene Kelley
    >>> Senior Open Source Software Engineer
    >>> Advanced Design Solutions Team
    >>> Network Solutions (MonsterCommerce)
    >>> Swansea, Illinois, USA
    >>>

    >>
    >> It's doing exactly what you told it to do - refresh sends the data to
    >> the server, and your script is processing it.
    >>
    >> After moving the uploaded file, use header('Location...") to redirect
    >> to a new page.
    >>

    > Thanks Jerry. I totally agree with you. Could require a design change
    > then I guess. Now that you've brought that to the table (I was thinking
    > the problem was just in the snippet of code I supplied), I should have
    > explained that this snippet of code is within a form which is submitting
    > to itself that is gathering other data which is then cleansed and
    > validated and then, if any errors, re-populates the elements and
    > provides suggested corrective actions.
    >
    > Although your suggestion is a good one and likely should have been
    > designed that way initially, it really gets me to thinking (as
    > programming often does) ... Is there really just no way to determine
    > that a variable is already set and is holding a value, so don't create
    > another one.... ???
    >
    > Thanks again for the good advice and bringing in a fresh new angle...
    >


    Well, you could set a value in the session to indicate the page has been
    processed. But then when do you reset that value?

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================


  7. Default Re: What am I missing here?

    In article <m7udnX_ePYsKuETanZ2dnUVZ_vrinZ2d@comcast.com>,
    jstucklex@attglobal.net says...
    > No it is not. Every time a page is called, it is a new execution of the
    > script. There is nothing remembered from previous runs of the script,
    > other than what is passed to the script, i.e. in the session, a cookie
    > or GET or POST parameters.
    >
    >
    >


    Just to add to jerrys comment its also worth remembering that a script
    "included" adds its variables to the globals array and will still have
    the variable set if (for some reason) it was included again.

    I mention it because I saw someone do that recently.

    I think that covers everything tho ;-)



  8. Default Re: What am I missing here?

    phpCodeHead wrote:
    > On Mar 13, 1:45 am, Tim Roberts <t...@probo.com> wrote:
    >> Gene Kelley <g...@bizflowdesigns.com> wrote:
    >>> Jerry Stuckle wrote:
    >>>> It's doing exactly what you told it to do - refresh sends the data to
    >>>> the server, and your script is processing it.
    >>>> After moving the uploaded file, use header('Location...") to redirect to
    >>>> a new page.
    >>> Thanks Jerry. I totally agree with you. Could require a design change
    >>> then I guess. Now that you've brought that to the table (I was thinking
    >>> the problem was just in the snippet of code I supplied), I should have
    >>> explained that this snippet of code is within a form which is submitting
    >>> to itself that is gathering other data which is then cleansed and
    >>> validated and then, if any errors, re-populates the elements and
    >>> provides suggested corrective actions.
    >>> Although your suggestion is a good one and likely should have been
    >>> designed that way initially, it really gets me to thinking (as
    >>> programming often does) ... Is there really just no way to determine
    >>> that a variable is already set and is holding a value, so don't create
    >>> another one.... ???

    >> To quote Jerry Stuckle from an earlier thread today: "Each time a script
    >> starts, it is a new program. It has no information from any previous
    >> script. The only way you can maintain values is through cookies or
    >> sessions."
    >> --
    >> Tim Roberts, t...@probo.com
    >> Providenza & Boekelheide, Inc.

    >
    > Maybe I as well am misunderstanding something in this post.
    >
    > So if a variable is NOT set. Then a function is called within the
    > script based on this false positive in that, say $x, if it's TRUE that
    > this variable ISN'T set then call a function that sets it, operates on
    > it, then returns whatever based on this operation (i.e. if(!
    > isset($x) ? functionThatDoesSetX() : moveAlongNotSettingX()
    >
    > At this point, if the script is reloaded, or even submitted in this
    > case as the form is submitting unto itself, isn't it true that now
    > this $x is set and functionThatDoesSetX() would NOT run?
    >
    > Interesting .... I will go learn and play in my sandbox for awhile.
    >
    >


    No it is not. Every time a page is called, it is a new execution of the
    script. There is nothing remembered from previous runs of the script,
    other than what is passed to the script, i.e. in the session, a cookie
    or GET or POST parameters.


    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================


  9. Default Re: What am I missing here?

    On Thu, 13 Mar 2008 18:37:35 +0100, Jerry Stuckle
    <jstucklex@attglobal.net> wrote:

    > good@respnse.sic.com wrote:
    >> In article <m7udnX_ePYsKuETanZ2dnUVZ_vrinZ2d@comcast.com>,
    >> jstucklex@attglobal.net says...
    >>> No it is not. Every time a page is called, it is a new execution of
    >>> the script. There is nothing remembered from previous runs of the
    >>> script, other than what is passed to the script, i.e. in the session,
    >>> a cookie or GET or POST parameters.
    >>>
    >>>
    >>>

    >> Just to add to jerrys comment its also worth remembering that a script
    >> "included" adds its variables to the globals array and will still have
    >> the variable set if (for some reason) it was included again.
    >> I mention it because I saw someone do that recently.
    >> I think that covers everything tho ;-)
    >>

    >
    > No, variables in an included file are not added to the globals array,
    > unless you specify them as global. They are, however, available after
    > the include to code not in a function.


    More precise: variables in an include are available within the scope the
    include is called. So if that's in global scope, they're global. In
    function / methods etc. they're usually limited to that scope unless made
    global manually.
    --
    Rik Wasmus

  10. Default Re: What am I missing here?

    good@respnse.sic.com wrote:
    > In article <m7udnX_ePYsKuETanZ2dnUVZ_vrinZ2d@comcast.com>,
    > jstucklex@attglobal.net says...
    >> No it is not. Every time a page is called, it is a new execution of the
    >> script. There is nothing remembered from previous runs of the script,
    >> other than what is passed to the script, i.e. in the session, a cookie
    >> or GET or POST parameters.
    >>
    >>
    >>

    >
    > Just to add to jerrys comment its also worth remembering that a script
    > "included" adds its variables to the globals array and will still have
    > the variable set if (for some reason) it was included again.
    >
    > I mention it because I saw someone do that recently.
    >
    > I think that covers everything tho ;-)
    >
    >
    >


    No, variables in an included file are not added to the globals array,
    unless you specify them as global. They are, however, available after
    the include to code not in a function.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================


+ Reply to Thread
Page 1 of 2 1 2 LastLast