PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/seo-title-tag/ajaximage.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 169 lines | 126 code | 37 blank | 6 comment | 19 complexity | 6c4e5b7fd16aefdf5e52c767f5cc1b24 MD5 | raw file
  1. <?php
  2. $path = dirname(__FILE__)."/uploads/";
  3. $valid_formats = array("csv");
  4. if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
  5. {
  6. $name = $_FILES['photoimg']['name'];
  7. $size = $_FILES['photoimg']['size'];
  8. if(strlen($name))
  9. {
  10. list($txt, $ext) = explode(".", $name);
  11. if(in_array($ext,$valid_formats))
  12. {
  13. if($size<(10240000000000*102400000000))
  14. {
  15. $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
  16. $tmp = $_FILES['photoimg']['tmp_name'];
  17. if(move_uploaded_file($tmp, $path.$actual_image_name))
  18. {
  19. //mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
  20. echo '<b>Uploaded File:</b>&nbsp;&nbsp;'.$actual_image_name ;
  21. if($actual_image_name){
  22. $file_name = $actual_image_name;
  23. $table_name = 'wp_postmeta';
  24. $column_string = 'post_id,meta_key,meta_value';
  25. $start_row = '2';
  26. function csv_to_db_get_abs_path_from_src_file($src_file)
  27. {
  28. if(preg_match("/http/",$src_file))
  29. {
  30. $path = parse_url($src_file, PHP_URL_PATH);
  31. $abs_path = $_SERVER['DOCUMENT_ROOT'].$path;
  32. $abs_path = realpath($abs_path);
  33. if(empty($abs_path)){
  34. $wpurl = get_bloginfo('wpurl');
  35. $abs_path = str_replace($wpurl,ABSPATH,$src_file);
  36. $abs_path = realpath($abs_path);
  37. }
  38. }
  39. else
  40. {
  41. echo $relative_path = $src_file;
  42. $abs_path = realpath($relative_path);
  43. }
  44. return $abs_path;
  45. }
  46. function readAndDump($src_file,$table_name,$column_string="",$start_row=2)
  47. {
  48. global $wpdb;
  49. $errorMsg = "";
  50. if(empty($src_file))
  51. {
  52. echo "empltyfilej el j";
  53. $errorMsg .= "<br />Input file is not specified";
  54. return $errorMsg;
  55. }
  56. echo $path;
  57. echo "File path= ". $file_path = 'http://localhost/trello/wp-content/plugins/seo-title-tag/uploads/1347020450ng.csv';
  58. $file_handle = fopen($file_path, "r");
  59. if ($file_handle === FALSE) {
  60. // File could not be opened...
  61. $errorMsg .= 'Source file could not be opened!<br />';
  62. $errorMsg .= "Error on fopen('$file_path')"; // Catch any fopen() problems.
  63. return $errorMsg;
  64. }
  65. $row = 1;
  66. while (!feof($file_handle) )
  67. {
  68. $line_of_text = fgetcsv($file_handle, 1024);
  69. if ($row < $start_row)
  70. {
  71. // Skip until we hit the row that we want to read from.
  72. $row++;
  73. continue;
  74. }
  75. die($row);
  76. $columns = count($line_of_text);
  77. echo "<br />Column Count: ".$columns."<br />";
  78. if ($columns >1)
  79. {
  80. $query_vals = "'".$wpdb->escape($line_of_text[0])."'";
  81. for($c=1;$c<$columns;$c++)
  82. {
  83. $line_of_text[$c] = utf8_encode($line_of_text[$c]);
  84. $line_of_text[$c] = addslashes($line_of_text[$c]);
  85. $query_vals .= ",'".$wpdb->escape($line_of_text[$c])."'";
  86. }
  87. echo "<br />Query Val: ".$query_vals."<br />";
  88. echo $query = "INSERT INTO $table_name ($column_string) VALUES ($query_vals)";
  89. die('fds');
  90. //echo "<br />Query String: ". $query;
  91. $results = $wpdb->query($query);
  92. if(empty($results))
  93. {
  94. $errorMsg .= "<br />Insert into the Database failed for the following Query:<br />";
  95. $errorMsg .= $query;
  96. }
  97. //echo "<br />Query result".$results;
  98. }
  99. $row++;
  100. }
  101. fclose($file_handle);
  102. return $errorMsg;
  103. }
  104. echo $errorMsg = readAndDump($file_name,$table_name,$column_string,$start_row);
  105. echo '<div id="message" class="updated fade"><p><strong>';
  106. if(empty($errorMsg))
  107. {
  108. echo 'File content has been successfully imported into the database!';
  109. }
  110. else
  111. {
  112. echo "Error occured while trying to import!<br />";
  113. echo $errorMsg;
  114. }
  115. echo '</strong></p></div>';
  116. }
  117. //echo "<img src='uploads/".$actual_image_name."' class='preview'>";
  118. }
  119. else
  120. echo "failed";
  121. }
  122. else
  123. echo "Image file size max 1 MB";
  124. }
  125. else
  126. echo "Invalid file format..";
  127. }
  128. else
  129. echo "Please Select CSV File..!";
  130. exit;
  131. }
  132. ?>