Results 1 to 5 of 5

Thread: PHP file upload/MySQL question

  1. PHP file upload/MySQL question

    I'm trying to make a script that will take an uploaded CSV file specified by the user and replace into an existing table. I tested the syntax in PHPMyAdmin because it has the function so that you can insert data from a text file into a table. Here's the form:

    Code:
    <div id="body"><form action="parts.php?action=parts" enctype="multipart/form-data" method="post">
    	<div id="updates">
    		<div class="header">Install Parts Update</div>
    		Use the button below to select a file for upload.<br /><br />
    		<input name="MAX_FILE_SIZE" type="hidden" value="5242880" /><input name="partslist" type="file" /><br />
    	</div>
    	<div id="buttons">
    		<input name="submit" type="submit" value="Cancel" /> <input name="submit" type="submit" value="Install Update" />
    	</div>
    </form></div>
    Here's the PHP code:

    Code:
    // This isn't working. I can't understand why.
    if( $_FILES['partslist']['name'] ) {
    	$sql_install = mysql_query( "LOAD DATA LOCAL INFILE '{$_FILES['partslist']['tmp_name']}' REPLACE INTO TABLE `ars_parts` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\r'" );
    	if( $sql_install ) { $info = "Parts update installed."; }
    	else { $info = 'Failed to install parts update: ' . mysql_error() . '.'; }
    }
    Every time it tells me "Failed to install parts update: The used command is not allowed with this MySQL version." But it lets me upload with PHPMyAdmin! I copied and pasted the line that PMA gives me on a successful insert. There's probably something simple that I'm missing but I just can't figure it out. Any help would be appreciated.

  2. word of advice: 5mb csv files aren't really the way to go if you're updating a database, trust me I've been there

    anyway try this

    $sql_install = mysql_query( "LOAD DATA LOCAL INFILE '{".$_FILES['partslist']['tmp_name']."}' REPLACE INTO TABLE `ars_parts` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\r'" );

    no idea if it'll work or not, but it's a pet peeve of mine to see variables passed in string arguments like that. If it doesn't, try copying the file to some temp file in your home directory, preferrably chmodded to 0777 (assuming this csv upload isn't public).

  3. Quote Originally Posted by cka
    word of advice: 5mb csv files aren't really the way to go if you're updating a database, trust me I've been there
    Oh, I know, but one day the files being put in may be that big. Right now they are small. If I can't get this to work, I guess I'll have to teach PMA to people. It's going to be hard enough writing directions on installing PHP/MySQL and configuring. Sheesh.

    anyway try this

    $sql_install = mysql_query( "LOAD DATA LOCAL INFILE '{".$_FILES['partslist']['tmp_name']."}' REPLACE INTO TABLE `ars_parts` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\r'" );

    no idea if it'll work or not, but it's a pet peeve of mine to see variables passed in string arguments like that. If it doesn't, try copying the file to some temp file in your home directory, preferrably chmodded to 0777 (assuming this csv upload isn't public).

    I should keep the {} brackets even though I'm passing the variable outside of the string?

    Also, I tried the move_uploaded_file and chmod methods and they didn't work. Here's hoping.

  4. oh, no get rid of those squiggly brackets

    if all else fails, break the uploaded file into an array with file() and manually REPLACE INTO though a while loop

  5. Oh god, fread.

    Looks like that's what I'll have to do.

    The "partno" field of the "parts" table is unique so I can just have it go through and read the file, then do insert intos. Ahh... fun times coding. Thanks for the help man.

    Why is it you don't like to pass the variable that way? To my inexperienced brain there is no difference but if there is a security reason or quality thing, I'd like to know.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Games.com logo