Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

File Renaming with CGI

WonTon2001WonTon2001 Posts: 11Member
I'm writing a perl script that manages a text file. I need to be able to rename a file within the perl script. I tried using rename but i keep getting 0 as a return value. Does anyone know of any other way to rename a file using perl as a CGI. I tried it like this:
rename "oldfilename", "newfilename";
Any help is greatly appreciated.

Comments

  • If your on a UNIX like system and have the right permissions

    [code]
    system("mv oldfile newfile");
    [/code]

    Or if your on windows

    [code]
    system("ren oldfile newfile");
    [/code]

    Hope this helps
  • WonTon2001WonTon2001 Posts: 11Member
    : If your on a UNIX like system and have the right permissions
    :
    : [code]
    : system("mv oldfile newfile");
    : [/code]
    :
    : Or if your on windows
    :
    : [code]
    : system("ren oldfile newfile");
    : [/code]
    :
    : Hope this helps
    :


    This approach also failed. The script will be running on a Red Hat box using Apache. Apache is setup to run as User nobody and Group nobody. However, my text file and my scripts are owned by my username. The text file is a listing of settings. I update the settings by opening the text file and a temp file. I write the updated settings to the temp file. I need to rename the temp file after I update it. If I can't rename the file, is there a better way to update it? Any help is greatly appriciated. Thanks
  • A much easier approach would be to use dbmopen(). This is a very simple database system and will not need any file renaming.

    [code]
    dbmopen(%db,"config",0666) || die("Cant open config database!
    ");
    $db{name} = "rinmak";
    delete($db{name});
    dbmclose(%db);
    [/code]

    This code will create a file in the current directory called something along the lines of config. This is the database or config file.

    By defining $db{name} you are creating a new field in the database which can then be accessed just like any other hash field. Even after the database has been closed and re-opened.

    When you call delete() with a hash field from the database it does what you would expect :).

    You may have to create the databasee with your permissions first then chmod rw-rw-rw- if your script cannot create files by itself.

    Need any more help, just ask
Sign In or Register to comment.